How to Count With Subquery

Asked

Viewed 640 times

0

I need to count table fields, however, these fields have different types of filters within Where. I need to be shown the consolidated information, I tried to do openquery but returns error.

select ct.contratante,
       ct.fase,
       (select  count(distinct cpf) from #info_acionamentos xx with(nolock) where  ct.status_alo = 1) as alo,
       (select  count(distinct cpf) from #info_acionamentos xx with(nolock) where  ct.status_cpc = 1) as cpc
from #info_acionamentos ct

inserir a descrição da imagem aqui

  • Makes a Union of the two selects that Voce commented, is the simplest way to do

  • 1

    Table fields would be the legs of the table, the chair? You didn’t put example of what you have on your tables and what you want the result.

  • What do you want to do? Count table fields? Fields have filters. It would be better to present your question better

1 answer

0


If I understood correctly the result you intended to get, the query could be written as follows:

select ct.contratante,
       ct.fase,
       count(distinct CASE ct.status_alo WHEN 1 THEN ct.cpf ELSE NULL END) alo,
       count(distinct CASE ct.status_cpc WHEN 1 THEN ct.cpf ELSE NULL END) cpc
from #info_acionamentos ct
group by ct.contratante, ct.fase

No need to use two queries in select.

  • Thank you very much, Excellent 100% functional response. While some have ability to analyze and help, others only criticize the post.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.