计算每小时使用日期列表的日期范围计数。

huangapple go评论96阅读模式
英文:

Compute the date range count for every hour using a list of dates

问题

  1. 我想要从日期列表中获得Map<String hours, count >
  2. 我还要按日期分组,每小时计数。

例如,在数据库中的值如下:

2023-07-18 10:10:00.000
2023-07-18 10:20:00.000
2023-07-18 11:10:00.000
2023-07-18 11:30:00.000
2023-07-18 11:35:00.000
2023-07-18 12:10:00.000
2023-07-18 12:30:00.000

我的预期结果应该如下所示,应该计算每个1小时范围的计数:

Map结果

&lt;10-11 ,2&gt;
&lt;11-12 ,3&gt;
&lt;12-13 ,2&gt;

我尝试了论坛提供的许多示例,但都无法正常工作。

英文:
  1. I want the Map<String hours, count > from the list of dates
  2. I also group by date and count for every 1 hour

Ex In DB Value looks like this

2023-07-18 10:10:00.000
2023-07-18 10:20:00.000
2023-07-18 11:10:00.000
2023-07-18 11:30:00.000
2023-07-18 11:35:00.000
2023-07-18 12:10:00.000
2023-07-18 12:30:00.000

My expected result should be as follow as should calculate each 1 hour range count

Map result

&lt;10-11 ,2&gt;
&lt;11-12 ,3&gt;
&lt;12-13 ,2&gt;

I tried lot of examples provide by the forum but does't work.

答案1

得分: 1

如果您期望在输出中看到<hour-range, count>,以下查询可能会解决您的问题 -

SELECT group_concat(distinct CONCAT(HOUR(date_time), ' - ', HOUR(date_time) + 1)) AS hour_range, COUNT(date_time) AS count
FROM your_table
GROUP BY HOUR(date_time)
ORDER BY HOUR(date_time);

Fiddle链接

英文:

In case you are expecting <hour-range, count> in your output, following query might solve your problem -

SELECT group_concat(distinct CONCAT(HOUR(date_time), &#39;-&#39;, HOUR(date_time) + 1)) AS hour_range, COUNT(date_time) AS count
FROM your_table
GROUP BY HOUR(date_time)
ORDER BY HOUR(date_time);

Fiddle Link

huangapple
  • 本文由 发表于 2023年7月18日 12:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定