我需要一份报告,其中包含列的不同值的计数,按日期分组,每个日期输出一行。
表格列是日期,位置ID(var)和评级(var)。评级只能有四个值中的一个,'A','B','C'或null /空白。我可以使用此选项为每个值的每个日期获得一行,但是如何为每个日期获取同一行上所有四个值的计数?
SELECT date, rating, count(rating) AS ratings
FROM table
GROUP BY date, rating
答案 0 :(得分:2)
将SUM
与布尔值一起使用,如下所示:
SELECT
date,
SUM(rating = 'A') AS count_A,
SUM(rating = 'B') AS count_B,
SUM(rating = 'C') AS count_C,
SUM(rating = '' OR rating IS NULL) AS count_blank
FROM yourtable
GROUP BY date