In Redshift/Postgres, how to count rows that meet a condition?

前端 未结 4 1357
猫巷女王i
猫巷女王i 2020-12-08 00:10

I\'m trying to write a query that count only the rows that meet a condition.

For example, in MySQL I would write it like this:

SELECT
    COUNT(IF(gr         


        
4条回答
  •  既然无缘
    2020-12-08 00:51

    Another method:

    SELECT 
        sum(CASE WHEN grade < 70 THEN 1 else 0 END) as grade_less_than_70,
        sum(CASE WHEN grade >= 70 and grade < 80 THEN 1 else 0 END) as grade_between_70_and_80
    FROM
       grades
    

    Works just fine in case you want to group the counts by a categorical column.

提交回复
热议问题