Ronni .Net Posted November 16, 2011 Report Share Posted November 16, 2011 Boa noite pessoal,Tenho uma tabela, onde tenho que fazer 3 count dentro da mesma apenas com parametros direfentes em cada count.Pensei em fazer com inner join.Segue a query:select distinct m1.[From], tudo.total, tarifadas.total, naoTarifadas.totalfrom messages m1inner join (select COUNT(*) as total from messages mx where mx.[status] <= 4 Group By mx.[From]) as tudoon m1.[From] = tudo.[From]inner join (select COUNT(*) as total from messages m2 where m2.[status] <= 4 and asd= 1 Group By m2.[From]) as tarifadas on m1.[From] = tarifadas.[From]inner join (select COUNT(*) as total from messages m3 where m3.[status] <= 4 and asd= 3 Group By m3.[From]) as naoTarifadason m1.[From] = naoTarifadas.[From]Group By m1.[From], tudo.total, tarifadas.total, naoTarifadas.totalOrder By m1.[From] Desco sql me diz que a coluna m1.[From] não existe mas se eu rodar apenas select distinct m1.[From] from messages m1 ela rodaO que eu to fazendo de errado?Obrigado galera! Quote Link to comment Share on other sites More sharing options...
0 fulvio Posted November 17, 2011 Report Share Posted November 17, 2011 Bom dia Ronni, A cláusula on m1.[From] = tudo.[From] está incorreta, pois o tudo é um alias (apenas um apelido). Da forma que fez, o tudo teria q ser uma variável q conteria o resultado do count. Quote Link to comment Share on other sites More sharing options...
0 Ronni .Net Posted November 17, 2011 Author Report Share Posted November 17, 2011 Valeu.Consegui a solução, na verdade eu estava errando no group by.aí está a soluçãoselect m1.[From], Count(*) as total, total2.total, total3.totalfrom messages m1inner join(select m2.[From], Count(*) as totalfrom messages m2where m2.[status] <= 4and m2.asd= 1group by m2.[From]) as total2on m1.[From] = total2.[From]inner join(select m3.[From], Count(*) as totalfrom messages m3where m3.[status] <= 4and m3.asd= 3group by m3.[From]) as total3on m1.[From] = total3.[From]group by m1.[From], total2.total, total3.totalGrande Abraço. Quote Link to comment Share on other sites More sharing options...
Question
Ronni .Net
Boa noite pessoal,
Tenho uma tabela, onde tenho que fazer 3 count dentro da mesma apenas com parametros direfentes em cada count.
Pensei em fazer com inner join.
Segue a query:
select distinct m1.[From], tudo.total, tarifadas.total, naoTarifadas.total
from messages m1
inner join (select COUNT(*) as total from messages mx where mx.[status] <= 4 Group By mx.[From]) as tudo
on m1.[From] = tudo.[From]
inner join (select COUNT(*) as total from messages m2 where m2.[status] <= 4 and asd= 1 Group By m2.[From]) as tarifadas
on m1.[From] = tarifadas.[From]
inner join (select COUNT(*) as total from messages m3 where m3.[status] <= 4 and asd= 3 Group By m3.[From]) as naoTarifadas
on m1.[From] = naoTarifadas.[From]
Group By m1.[From], tudo.total, tarifadas.total, naoTarifadas.total
Order By m1.[From] Desc
o sql me diz que a coluna m1.[From] não existe mas se eu rodar apenas select distinct m1.[From] from messages m1 ela roda
O que eu to fazendo de errado?
Obrigado galera!
Link to comment
Share on other sites
2 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.