1

嗨,我有一张这样的桌子

Activity     Date
---------------------
0          2012-11-20
1          2012-11-20
1          2012-11-20
1          2012-11-21
1          2012-11-21
0          2012-11-22

我需要选择记录为

 Day-wise total   0's    1's    Date  
------------------------------------------
     3             1      2     2012-11-20
     2             0      2     2012-11-21
     1             1      0     2012-11-22
4

1 回答 1

4

您需要的是一个GROUP BY date,COUNT(Activity)来获得按日总计和一个CASE表达式来旋转 1 和 0 的值。我SUM在这里使用过,但它的行为是COUNT这样的:

SELECT
  COUNT(Activity) AS "Day-wise total",
  SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) AS "0's",
  SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) AS "1's",
  `Date` AS "Date"
FROM Table
GROUP BY `Date`;

SQL 小提琴演示

于 2012-11-22T10:15:36.130 回答