我想通过使用第二组来计数。我有这样的数据:
id sectionid date
1 1 2015-09-16
2 1 2015-09-16
3 2 2015-09-16
4 2 2015-09-16
5 3 2015-09-16
6 1 2015-09-17
7 2 2015-09-18
8 2 2015-09-18
结果将是:
Date section1count section2count section3count
2015-09-16 2 2 1
2015-09-17 1 0 0
2015-09-18 0 2 0
提前致谢。
答案 0 :(得分:2)
您可以结合使用SUM()
函数和GROUP BY
来获取所需的结果集:
SELECT date AS Date,
SUM(CASE WHEN sectionid=1 THEN 1 ELSE 0 END) AS section1count,
SUM(CASE WHEN sectionid=2 THEN 1 ELSE 0 END) AS section2count,
SUM(CASE WHEN sectionid=3 THEN 1 ELSE 0 END) AS section3count
FROM table
GROUP BY date
答案 1 :(得分:0)
她是我的结果。您可以将其与每个部分进行比较
SELECT
SUM(IF( m.sectionid = 1 , 1,0)) section1count,
SUM(IF( m.sectionid = 2 , 1,0)) section2count,
SUM(IF( m.sectionid = 3 , 1,0)) section3count,
m.date
FROM myt m
GROUP BY m.`date`;