如何选择按一天中的时间分组的记录,包括没有记录的时间

发布于 2024-09-13 10:14:45 字数 1268 浏览 5 评论 0原文

我在 Oracle 数据库中有一个表,其中包含用户在我们的系统之一中执行的操作。它用于统计分析,我需要显示给定日期执行的操作数量,按一天中的小时分组。

我有一个查询可以很好地做到这一点,但是,它不会显示一天中不包含任何操作的时间(显然是因为该小时没有可显示的记录)。

例如,查询:(

SELECT TO_CHAR(event_date, 'HH24') AS during_hour,
  COUNT(*)
FROM user_activity
WHERE event_date BETWEEN to_date('15-JUN-2010 14:00:00', 'DD-MON-YYYY HH24:MI:SS') 
    AND to_date('16-JUN-2010 13:59:59', 'DD-MON-YYYY HH24:MI:SS')
AND event = 'user.login'
GROUP BY TO_CHAR(event_date, 'HH24')
ORDER BY during_hour;

不要注意奇怪的开始和结束日期,它们是这样的,因为它们位于 GMT,而我是 GMT+10 - 但这是一个单独的问题,除非它影响回答此问题的能力问题)

这会产生一个结果集:

DURING_HOUR            COUNT(*)               
---------------------- ---------------------- 
00                     12                     
01                     30                     
02                     18                     
03                     20                     
04                     12                     
05                     24                     
06                     20                     
07                     4                      
23                     8                      

9 rows selected

如何修改此查询以显示一天中包含 0 个事件的时间?

我搜索了 Stack Overflow,发现了一些类似的问题,但没有回答我的具体问题。

感谢您的帮助。

I have a table in an Oracle database that contains actions performed by users in one of our systems. It's used for statistical analysis and I need to display the number of actions performed for a given date, grouped by hour of the day.

I have a query that does that fine, however, it does not display the hours of the day that contain no actions (obviously because there are no records to display for that hour).

For example, the query:

SELECT TO_CHAR(event_date, 'HH24') AS during_hour,
  COUNT(*)
FROM user_activity
WHERE event_date BETWEEN to_date('15-JUN-2010 14:00:00', 'DD-MON-YYYY HH24:MI:SS') 
    AND to_date('16-JUN-2010 13:59:59', 'DD-MON-YYYY HH24:MI:SS')
AND event = 'user.login'
GROUP BY TO_CHAR(event_date, 'HH24')
ORDER BY during_hour;

(Pay no attention to the odd start and end dates, they are like this because they're in GMT and I'm GMT+10 - but that's a separate issue, unless it affects the ability to answer this question)

This produces a result set of:

DURING_HOUR            COUNT(*)               
---------------------- ---------------------- 
00                     12                     
01                     30                     
02                     18                     
03                     20                     
04                     12                     
05                     24                     
06                     20                     
07                     4                      
23                     8                      

9 rows selected

How can I ammend this query to display the hours of the day that contain 0 events?

I've searched Stack Overflow, and found some similar questions, but not that answer my specific issue.

Your assistance is appreciated.

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

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

发布评论

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

评论(2

在梵高的星空下 2024-09-20 10:14:45
SELECT h.hrs, NVL(Quantity, 0) Quantity
FROM (SELECT TRIM(to_char(LEVEL - 1, '00')) hrs
       FROM dual
       CONNECT BY LEVEL < 25) h
LEFT JOIN (SELECT TO_CHAR(event_date, 'HH24') AS during_hour,
                  COUNT(*) Quantity
           FROM user_activity u
           WHERE event_date BETWEEN
                 to_date('15-JUN-2010 14:00:00', 'DD-MON-YYYY HH24:MI:SS') AND
                 to_date('16-JUN-2010 13:59:59', 'DD-MON-YYYY HH24:MI:SS')
           AND event = 'user.login'
           GROUP BY TO_CHAR(event_date, 'HH24')) t
ON (h.hrs = t.during_hour)
ORDER BY h.hrs;
SELECT h.hrs, NVL(Quantity, 0) Quantity
FROM (SELECT TRIM(to_char(LEVEL - 1, '00')) hrs
       FROM dual
       CONNECT BY LEVEL < 25) h
LEFT JOIN (SELECT TO_CHAR(event_date, 'HH24') AS during_hour,
                  COUNT(*) Quantity
           FROM user_activity u
           WHERE event_date BETWEEN
                 to_date('15-JUN-2010 14:00:00', 'DD-MON-YYYY HH24:MI:SS') AND
                 to_date('16-JUN-2010 13:59:59', 'DD-MON-YYYY HH24:MI:SS')
           AND event = 'user.login'
           GROUP BY TO_CHAR(event_date, 'HH24')) t
ON (h.hrs = t.during_hour)
ORDER BY h.hrs;
甜妞爱困 2024-09-20 10:14:45

一种选择是创建一个包含 24 行的单独表:00 --> 23.

然后对它进行外连接。

One option would be to create a separate table with 24 rows: 00 --> 23.

Then outer join against it.

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