› Fóruns › SQL e PL/SQL › Como montar essa SQL? › Como montar essa SQL?
Reabrindo este tópico.
Amigo, boa tarde
Notei um erro no SQL enviado no ultimo tópico. Ele está me exibindo valores do mes que subsequente ao escolhido.
Exemplo. O usuário irá escolher Janeiro / 2010, é para exibir Janeiro E Dezembro, mas está exibindo Janeiro E Fevereiro / 2010 E Dezembro de 2009.
SQL
select t.Loja, t.Oracle, T.vv, t.contratos, t.pedant, t.pedatual,
(vv + contratos) - oracle - pedant + pedatual conferencia
from (select trunc(dtvenda, 'MM') dtvenda,
faturado,
c.loja,
nvl(sum(c.vlrcurso), 0) oracle,
sum(case when length(contrato) = 9
then c.vlrcurso else 0 end) contratos,
first_value(sum(c.vlrcurso)) over (partition by loja order by
case when faturado = 'N' and to_char(dtvenda, 'MM/YYYY') = '12/2009'
then 0 else 1 end) pedant,
first_value(sum(c.vlrcurso)) over (partition by loja order by
case when faturado = 'N'
and to_char(dtvenda, 'MM/YYYY') = '01/2010'
then 0 else 1 end)pedatual
from cybelar_crescabr c
group by loja, trunc(dtvenda, 'MM'), to_char(dtvenda, 'MM/YYYY'), faturado) t
where to_char(dtvenda, 'MM/yyyy') = '01/2010'
and faturado = 'S'