MySQL 查询按日期分组(12 小时间隔)

发布于 2025-01-20 21:05:11 字数 781 浏览 0 评论 0原文

我编写了一个sql查询来获取在一段时间内创建的用户数量以绘制图形(grafana或chart js),我的sql查询是

 SELECT
  date(user.created_date) as "time",
  count(distinct user.id) as Number Of User,
  status as status
FROM user
WHERE
  created_date >= FROM_UNIXTIME(1649635200) AND 
  created_date < FROM_UNIXTIME(1649894399)
GROUP BY user.status, date(user.created_date)
ORDER BY date(user.created_date)

在这个查询中创建日期是从前端动态传递的, 现在我得到的结果是这样的 输入图片这里的描述

现在,每当我选择过去 24 小时/12 小时的日期过滤器时,有些结果就不存在,

有没有办法修改我的 sql 查询,以按 12 小时间隔的created_date 进行分组 例如,现在查询结果是 11/04/2022 - 5 个用户(已创建应用程序) 我想要这样的查询结果 11/04/2022 00:00:00 2 - 2 个用户创建于 11/04/2022 12:00:00 - 已创建 3 个用户

I wrote a sql query for getting number of users created in a period of time for plotting graph (grafana or chart js) , and my sql query is

 SELECT
  date(user.created_date) as "time",
  count(distinct user.id) as Number Of User,
  status as status
FROM user
WHERE
  created_date >= FROM_UNIXTIME(1649635200) AND 
  created_date < FROM_UNIXTIME(1649894399)
GROUP BY user.status, date(user.created_date)
ORDER BY date(user.created_date)

Here in this query created date is passed dynamically from front-end,
Now i am getting the result like,
enter image description here

Now whenever i select the date filter from last 24 hours/12 hours some of the result is not there,

Is there is any way to modify my sql query to group by created_date with 12 hour interval
For Example, Now query result is 11/04/2022 - 5 Users(Application Created) I want query result like this 11/04/2022 00:00:00 2 - 2 users created 11/04/2022 12:00:00 - 3 users created

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏雨一样淡蓝的深情 2025-01-27 21:05:11

在grafana中有一个归档的$__timeFrom()$__timeTo()
在此基础上我重写了我的查询:

SELECT
  (CASE 
    WHEN HOUR(TIMEDIFF($__timeFrom(), $__timeTo())) <= 24
    THEN user.created_date
    ELSE date(user.created_date) end) AS "time",
  count(distinct user.id) as Users,
FROM user
WHERE
  user.created_date >= $__timeFrom() AND 
  user.created_date < $__timeTo() AND
GROUP BY CASE
  when HOUR(TIMEDIFF($__timeFrom(), $__timeTo())) <= 24
  then user.created_date
  else date(created_date) end
ORDER BY CASE
  when HOUR(TIMEDIFF($__timeFrom(), $__timeTo())) <= 24
  then user.created_date
  else date(created_date) end;

In grafana there is a filed $__timeFrom() and $__timeTo()
On the basis of this I rewrite my query:

SELECT
  (CASE 
    WHEN HOUR(TIMEDIFF($__timeFrom(), $__timeTo())) <= 24
    THEN user.created_date
    ELSE date(user.created_date) end) AS "time",
  count(distinct user.id) as Users,
FROM user
WHERE
  user.created_date >= $__timeFrom() AND 
  user.created_date < $__timeTo() AND
GROUP BY CASE
  when HOUR(TIMEDIFF($__timeFrom(), $__timeTo())) <= 24
  then user.created_date
  else date(created_date) end
ORDER BY CASE
  when HOUR(TIMEDIFF($__timeFrom(), $__timeTo())) <= 24
  then user.created_date
  else date(created_date) end;
随波逐流 2025-01-27 21:05:11

如果您通过在组中使用此扩展,则将获得12小时的分组。

DATE(created_date) + INTERVAL (HOUR(created_date) - HOUR(created_date) MOD 12) HOUR

如果您拥有PRIV,则可以声明存储的功能,以使其更易于阅读。

DELIMITER $
DROP FUNCTION IF EXISTS TRUNC_HALFDAY$
CREATE
  FUNCTION TRUNC_HALFDAY(datestamp DATETIME)
  RETURNS DATETIME DETERMINISTIC NO SQL
  COMMENT 'truncate to 12 hour boundary. Returns the nearest
           preceding half-day (noon, or midnight)'
  RETURN DATE(datestamp) +
                INTERVAL (HOUR(datestamp) -
                          HOUR(datestamp) MOD 12) HOUR$
DELIMITER ;

您也可以执行此操作

SELECT
  TRUNC_HALFDAY(user.created_date) as "time",
  count(distinct user.id) as Number Of User,
  status as status
FROM user
WHERE
  created_date >= whatever AND 
  created_date < whatever
GROUP BY user.status, TRUNC_HALFDAY(user.created_date)
ORDER BY TRUNC_HALFDAY(user.created_date)

然后,即使该功能在查询中出现3次,

,因为它已声明为确定性它只会调用一次每行一次。更完整的写入在这里

If you use this expresion in your GROUP BY, you'll get a 12-hour grouping.

DATE(created_date) + INTERVAL (HOUR(created_date) - HOUR(created_date) MOD 12) HOUR

You can, if you have the priv, declare a stored function to make this easier to read.

DELIMITER $
DROP FUNCTION IF EXISTS TRUNC_HALFDAY$
CREATE
  FUNCTION TRUNC_HALFDAY(datestamp DATETIME)
  RETURNS DATETIME DETERMINISTIC NO SQL
  COMMENT 'truncate to 12 hour boundary. Returns the nearest
           preceding half-day (noon, or midnight)'
  RETURN DATE(datestamp) +
                INTERVAL (HOUR(datestamp) -
                          HOUR(datestamp) MOD 12) HOUR$
DELIMITER ;

Then you can do

SELECT
  TRUNC_HALFDAY(user.created_date) as "time",
  count(distinct user.id) as Number Of User,
  status as status
FROM user
WHERE
  created_date >= whatever AND 
  created_date < whatever
GROUP BY user.status, TRUNC_HALFDAY(user.created_date)
ORDER BY TRUNC_HALFDAY(user.created_date)

Even though the function appears three times in your query, because it's declared DETERMINISTIC it only gets called once per row.

More complete writeup here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文