SQL: Count distinct values from one column based on multiple criteria in other columns

前端 未结 1 1160
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 17:04

I am trying to do count distinct values based on multiple criteria. Sample data exercise included below.

           Table1
╔════════╦════════╦═══         


        
1条回答
  •  孤城傲影
    2020-12-10 17:20

    You can have a conditional count(distinct) by using this code:

    SELECT Test, COUNT(DISTINCT "Bug ID") AS "Total Bugs",
    count(distinct (CASE WHEN "Status" <> 'Closed' THEN "Bug ID" END)) as "Open Bugs"
    FROM Table1
    GROUP BY Test
    

    The case statement checks the condition. When true, it returns the Bug ID. When not present, it defaults to NULL, so the id does not get counted.

    0 讨论(0)
提交回复
热议问题