0

我需要一份报告,其中包含按日期分组的列具有的不同值的计数,每个日期一个输出行。

表格列是日期、位置 ID (var) 和评级 (var)。评级只能有四个值之一,“A”、“B”、“C”或空/空白。使用此选择,我可以在每个日期每个值获取一行,但是如何获取每个日期在同一行上的所有四个值的计数?

SELECT date, rating, count(rating) AS ratings
FROM table
GROUP BY date, rating
4

1 回答 1

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
于 2012-07-25T20:55:25.123 回答