euronitos Posted April 24, 2015 at 01:54 PM Report Share #581792 Posted April 24, 2015 at 01:54 PM Boas, Estou a tentar uma query em SQL que permita extrair do SQL Server vendas por cliente, mas distribuídas pelos anos. Seria algo do género: Cliente | 2013 | 2014 | 2015 | Total - Cliente Y | 20€ | 20€ | 20€ | 60 Cliente X | 10€ | 20€ | 0€ | 30€ - As minhas tabelas estão divididas por Encomendas, Produtos (com os precos) e Clientes. Isto é possível fazer numa única query? Obrigado desde já! Link to comment Share on other sites More sharing options...
pc.cesar Posted May 13, 2015 at 11:15 AM Report Share #583098 Posted May 13, 2015 at 11:15 AM (edited) select ,str(aa.ano) as Ano, cliente, ANULADO ,sum(aa.jan) as Janeiro ,sum(aa.fev) as Fevereiro ,sum(aa.mar) as Março ,sum(aa.abr) as Abril ,sum(aa.mai) as Maio ,sum(aa.jun) as Junho ,sum(aa.jul) as Julho ,sum(aa.ago) as Agosto ,sum(aa.stb) as Setembro ,sum(aa.out) as Outubro ,sum(aa.nov) as Novembro ,sum(aa.dez) as Dezembro ,sum(aa.tot) as Total from ( Select ft.ftano ano, ft.cliente, FT.ANULADO ,case when month(ft.fdata) = 1 then sum(FT.ETTILIQ) else 0 end as jan ,case when month(ft.fdata) = 2 then sum(FT.ETTILIQ) else 0 end as fev ,case when month(ft.fdata) = 3 then sum(FT.ETTILIQ) else 0 end as mar ,case when month(ft.fdata) = 4 then sum(FT.ETTILIQ) else 0 end as abr ,case when month(ft.fdata) = 5 then sum(FT.ETTILIQ) else 0 end as mai ,case when month(ft.fdata) = 6 then sum(FT.ETTILIQ) else 0 end as jun ,case when month(ft.fdata) = 7 then sum(FT.ETTILIQ) else 0 end as jul ,case when month(ft.fdata) = 8 then sum(FT.ETTILIQ) else 0 end as ago ,case when month(ft.fdata) = 9 then sum(FT.ETTILIQ) else 0 end as stb ,case when month(ft.fdata) = 10 then sum(FT.ETTILIQ) else 0 end as out ,case when month(ft.fdata) = 11 then sum(FT.ETTILIQ) else 0 end as nov ,case when month(ft.fdata) = 12 then sum(FT.ETTILIQ) else 0 end as dez ,case when month(ft.fdata) <= 12 then sum(FT.ETTILIQ) else 0 end as tot from FT (nolock) inner join cl (nolock) on ft.no = cl.no and ft.estab = cl.estab where FT.ETTILIQ <> 0 and ft.dilnoplano<>0 and ft.anulado=0 and ft.ftano=2012 group by ft.ftano,ft.nmdoc, ft.fdata, FT.ANULADO) as aa group by aa.ano, nmdoc, ANULADO Edited May 19, 2015 at 10:51 AM by apocsantos geshi César Faria Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now