如何统计一个日期内所有重复5次以上的记录
how to count all records that are repeated more than 5 times in a date
我试图在按用户分组的单个记录中获取在某个日期重复超过 5 次的记录的总和
这是我的查询
select user_id, answer_date, count(*) as total_mayor_que_5
from items
WHERE items.proyecto_id = 6
and `estado` IN (1,4)
AND `liquidada` = 1
and items.answer_date BETWEEN '2021-01-01' and '2021-12-31'
and items.user_id = 832
GROUP BY user_id, answer_date
HAVING COUNT(answer_date) >= 5
结果有这个
user_id
answered_date
more_greater_than_5
832
2021-11-08
6
832
2021-11-09
6
833
2021-11-09
6
833
2021-11-09
5
我不希望它显示所有这些记录;相反,我想要一个总数。例如,对于 ID 为 832 的用户,总数为 12。
这是我的结构 table 项
我想要这样的东西
user_id
total
832
12
833
11
有人可以帮助我吗?谢谢
您想要多一层分组
select user_id, sum(total_mayor_que_5) total
from (
select user_id, answer_date, count(*) as total_mayor_que_5
from items
WHERE items.proyecto_id = 6
and `estado` IN (1,4)
AND `liquidada` = 1
and items.answer_date BETWEEN '2021-01-01' and '2021-12-31'
and items.user_id = 832
GROUP BY user_id, answer_date
HAVING COUNT(answer_date) >= 5
) t
GROUP BY user_id
order by user_id
我试图在按用户分组的单个记录中获取在某个日期重复超过 5 次的记录的总和
这是我的查询
select user_id, answer_date, count(*) as total_mayor_que_5
from items
WHERE items.proyecto_id = 6
and `estado` IN (1,4)
AND `liquidada` = 1
and items.answer_date BETWEEN '2021-01-01' and '2021-12-31'
and items.user_id = 832
GROUP BY user_id, answer_date
HAVING COUNT(answer_date) >= 5
结果有这个
user_id | answered_date | more_greater_than_5 |
---|---|---|
832 | 2021-11-08 | 6 |
832 | 2021-11-09 | 6 |
833 | 2021-11-09 | 6 |
833 | 2021-11-09 | 5 |
我不希望它显示所有这些记录;相反,我想要一个总数。例如,对于 ID 为 832 的用户,总数为 12。
这是我的结构 table 项
我想要这样的东西
user_id | total |
---|---|
832 | 12 |
833 | 11 |
有人可以帮助我吗?谢谢
您想要多一层分组
select user_id, sum(total_mayor_que_5) total
from (
select user_id, answer_date, count(*) as total_mayor_que_5
from items
WHERE items.proyecto_id = 6
and `estado` IN (1,4)
AND `liquidada` = 1
and items.answer_date BETWEEN '2021-01-01' and '2021-12-31'
and items.user_id = 832
GROUP BY user_id, answer_date
HAVING COUNT(answer_date) >= 5
) t
GROUP BY user_id
order by user_id