SQL - Problem naming variables [closed]

-1

As I can not explain my problem well, I'll give you an example:

select cod_prod, sum(quantidade) as "Quantidade 2013", sum(quantidade) as "Quantidade 2014"
from itens
group by cod_prod
having "Quantidade 2013" in ...

The above example is not working because it does not recognize the name "Quantity 2013".

My question is:

How do I arrange a way to differentiate one sum from the other so that I can manipulate them differently in the having method?

Details:

    
asked by anonymous 31.10.2016 / 01:45

1 answer

2

Dude, in that case, I would tell you to make the sums separately, and then match the results you have in your main selection.

Something like this:

select cod_prod, s1.quantidade_soma as "Quantidade 2013", s1.quantidade_soma as "Quantidade 2014"
from itens i
join ( /*seleção em que você passará o ano e o código do produto*/
        select cod_prod, sum(quantidade) as quantidade_soma, 
        from itens i1
        where to_char(data_enc,'YYYY') = '2014'
        group by cod_prod
) s1 on (s1.cod_prod = i.cod_prod) 
/* repetir para 2013*/
where s1.quantidade_soma > s2.quantidade_soma 

I would still tell you first to filter, and in place of from itens i principal, already bring the data he asks for in the statement.

Any questions, just talk.

    
31.10.2016 / 03:13