我是sql noob,我有一个有3列的表
<table>
<tr>msg_date, msg_type, msg_flag</tr>
<tr>20170207 A 0</tr>
20170207 B 0
20170207 B 0
20170207 A 1
20170207 A 1
20170207 A 1
20170207 C 7
20170207 C 7
20170207 C 7
</table>
我想查询并显示以下内容
msg_date,&#34;此msg_date的行数,其中msg_flag == 0&#34;,&#34;此msg_date的行数,其中msg_type为C&#34;
有人可以帮忙吗
答案 0 :(得分:1)
您可以在此处使用条件聚合:
SELECT msg_date,
SUM(CASE WHEN msg_flag = 0 THEN 1 ELSE 0 END) AS zero_count,
SUM(CASE WHEN msg_type = 'C' THEN 1 ELSE 0 END) AS c_count
FROM yourTable
GROUP BY msg_date