表格结构
id(int)
dated_on(datetime)
(many_more)
包含数千个按ID分组的数据的表。
I want result as id, count of that id,
also count of id having date difference is week.(i.e. count of data last week)
答案 0 :(得分:1)
select
id,
count(id) as cnt,
sum(if(now() - interval 7 day <= dated_on,1,0)) as lastweek
from table
group by id
答案 1 :(得分:0)
SELECT
id,
COUNT(*),
SUM(CASE WHEN dated_on = CURDATE() - 7 THEN 1 ELSE 0 END)
FROM
yourTable
GROUP BY
id
答案 2 :(得分:0)
我也有解决方案:
SELECT id, count(id) as counter,
COUNT(CASE WHEN DATEDIFF( CURDATE(), dated_on ) < 7 THEN 1 ELSE NULL END) AS latest_count
FROM my_table
GROUP BY id