我有两列,一个时间戳和一个已选择卡的号码。
card added
1 2016-09-23 13:48:48
3 2016-09-23 13:48:48
1 2016-09-23 13:48:48
2 2016-09-24 13:48:48
1 2016-09-24 13:48:48
2 2016-09-24 13:48:48
3 2016-09-24 13:48:48
我需要的输出是每张卡的数量,按条目分组,如下所示:
card count added
1 2 2016-09-23
3 1 2016-09-23
1 1 2016-09-24
2 2 2016-09-24
3 1 2016-09-24
所以我想我必须计算添加的卡是相同的。但是我不知道怎么做,而且我需要按照每个添加的卡进行分组。
答案 0 :(得分:2)
您需要按照卡片和截断日期对结果进行分组。可以通过调用date()
:
SELECT card, DATE(added) AS added, COUNT(*)
FROM mytable
GROUP BY card, DATE(added)
答案 1 :(得分:0)
您只需要GROUP BY
卡片和日期:
SELECT card, COUNT(*), CAST(added AS date) added
FROM your_table
GROUP BY card, CAST(added AS date)
答案 2 :(得分:0)
SELECT card, COUNT(card) AS CNT, added
FROM `example_table`
GROUP BY card, added
ORDER BY added ASC, card ASC