Jump to content
Fórum Script Brasil
  • 0

(Resolvido) inner join com Count


Ronni .Net

Question

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

  • 0

Valeu.

Consegui a solução, na verdade eu estava errando no group by.

aí está a solução

select m1.[From], Count(*) as total, total2.total, total3.total

from messages m1

inner join

(select m2.[From], Count(*) as total

from messages m2

where m2.[status] <= 4

and m2.asd= 1

group by m2.[From]) as total2

on m1.[From] = total2.[From]

inner join

(select m3.[From], Count(*) as total

from messages m3

where m3.[status] <= 4

and m3.asd= 3

group by m3.[From]) as total3

on m1.[From] = total3.[From]

group by m1.[From], total2.total, total3.total

Grande Abraço.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...