SQL 2 counts with different filter

后端 未结 6 1280
故里飘歌
故里飘歌 2020-12-21 02:56

I have a table and I need calculate two aggregate functions with different conditions in one statement. How can I do this?

Pseudocode below:

SELECT c         


        
6条回答
  •  無奈伤痛
    2020-12-21 03:27

    select '< 0' as filter, COUNT(0) as cnt from TableA where [condition 1]
    union
    select '> 0' as filter, COUNT(0) as cnt from TableA where [condition 2]
    

    Be sure that condition 1 and condition 2 create a partition on the original set of records, otherwise same records could be counted in both groups.

提交回复
热议问题