【问题标题】:how to count all records that are repeated more than 5 times in a date如何计算日期中重复超过 5 次的所有记录
【发布时间】:2022-01-28 15:38:51
【问题描述】:

我试图在按用户分组的单个记录中获取在某个日期重复超过 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。

这是我的结构表项

我想要这样的东西

user_id total
832 12
833 11

有人可以帮助我吗?谢谢

【问题讨论】:

  • 你的 DBMS 是什么?
  • 请将数据发布为文本而不是图像,并将所需的输出添加到问题中。
  • @Serg 我的 DBSM 是 MySQL 5.6(phpmyadmin)
  • 你想要的输出是什么?如果您想查看用户 832 和总计 63,请围绕您已有的内容进行查询。
  • @Isolated 编辑我的答案并放在上面,因为我想要查询输出。我正在努力做你刚刚告诉我的事情

标签: mysql sql phpmyadmin


【解决方案1】:

你想要多一层的分组

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

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2023-04-03
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多