Oracle SQL 查询显示每日事件计数和过去 7 天的事件总数

发布于 2024-09-15 09:47:40 字数 1328 浏览 6 评论 0原文

有下表:event_table,其中包含object_nameevent_numberevent_supplementary_infoevent_time的信息。我想要一个 sql 查询,它显示过去 7 天每天的事件数和总数。

我需要这样的东西

select Object_name, event_number
max(decode(trim(dow),'MONDAY',totalquantity,0)) Mon, 
max(decode(trim(dow),'TUESDAY',totalquantity,0)) Tue, 
max(decode(trim(dow),'WEDNESDAY',totalquantity,0)) Wed, 
max(decode(trim(dow),'THURSDAY',totalquantity,0)) Thu, 
max(decode(trim(dow),'FRIDAY',totalquantity,0)) Fri,
max(decode(trim(dow),'SATURDAY',totalquantity,0)) Sat, 
max(decode(trim(dow),'SUNDAY',totalquantity,0)) Sun,  
( 
max(decode(trim(dow),'MONDAY',totalquantity,0)) + 
max(decode(trim(dow),'TUESDAY',totalquantity,0)) + 
max(decode(trim(dow),'WEDNESDAY',totalquantity,0)) + 
max(decode(trim(dow),'THURSDAY',totalquantity,0)) + 
max(decode(trim(dow),'FRIDAY',totalquantity,0)) +
max(decode(trim(dow),'SATURDAY',totalquantity,0)) + 
max(decode(trim(dow),'SUNDAY',totalquantity,0)) 
) TOTAL 
from  
( 
  select event_name,  
  to_char(event_time, 'DAY') as dow,  
  sum(event_time) as totalquantity 
  from event_table a 
  where a.event_time >= trunc(sysdate-7,'D') 
  and a.tradedate <= trunc(sysdate-7,'D') + 7 
  group by a.event_name, alarm_time
) 
group by Object_name, event_number;

There is the following table: event_table, which contains information about object_name, event_number, event_supplementary_info, event_time. I would like to have a sql query, which shows number of events per last seven day daily and total number.

I need something like this

select Object_name, event_number
max(decode(trim(dow),'MONDAY',totalquantity,0)) Mon, 
max(decode(trim(dow),'TUESDAY',totalquantity,0)) Tue, 
max(decode(trim(dow),'WEDNESDAY',totalquantity,0)) Wed, 
max(decode(trim(dow),'THURSDAY',totalquantity,0)) Thu, 
max(decode(trim(dow),'FRIDAY',totalquantity,0)) Fri,
max(decode(trim(dow),'SATURDAY',totalquantity,0)) Sat, 
max(decode(trim(dow),'SUNDAY',totalquantity,0)) Sun,  
( 
max(decode(trim(dow),'MONDAY',totalquantity,0)) + 
max(decode(trim(dow),'TUESDAY',totalquantity,0)) + 
max(decode(trim(dow),'WEDNESDAY',totalquantity,0)) + 
max(decode(trim(dow),'THURSDAY',totalquantity,0)) + 
max(decode(trim(dow),'FRIDAY',totalquantity,0)) +
max(decode(trim(dow),'SATURDAY',totalquantity,0)) + 
max(decode(trim(dow),'SUNDAY',totalquantity,0)) 
) TOTAL 
from  
( 
  select event_name,  
  to_char(event_time, 'DAY') as dow,  
  sum(event_time) as totalquantity 
  from event_table a 
  where a.event_time >= trunc(sysdate-7,'D') 
  and a.tradedate <= trunc(sysdate-7,'D') + 7 
  group by a.event_name, alarm_time
) 
group by Object_name, event_number;

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

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

发布评论

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

评论(2

似梦非梦 2024-09-22 09:47:40

这是使用此示例数据的尝试。下次请您自行提供一些样本数据。

SQL> create table event_table (event_number,object_name,event_time,event_supplementary_info)
  2  as
  3  select 1, 'A', sysdate - 7, 'info' from dual union all
  4  select 1, 'B', sysdate - 7, 'info' from dual union all
  5  select 1, 'B', sysdate - 7, 'info' from dual union all
  6  select 2, 'A', sysdate - 6, 'info' from dual union all
  7  select 2, 'B', sysdate - 6, 'info' from dual union all
  8  select 2, 'B', sysdate - 6, 'info' from dual union all
  9  select 3, 'A', sysdate - 5, 'info' from dual union all
 10  select 3, 'A', sysdate - 5, 'info' from dual union all
 11  select 4, 'C', sysdate - 4, 'info' from dual union all
 12  select 4, 'C', sysdate - 4, 'info' from dual union all
 13  select 4, 'C', sysdate - 4, 'info' from dual union all
 14  select 4, 'C', sysdate - 4, 'info' from dual union all
 15  select 4, 'C', sysdate - 4, 'info' from dual union all
 16  select 4, 'D', sysdate - 4, 'info' from dual union all
 17  select 5, 'A', sysdate - 3, 'info' from dual union all
 18  select 6, 'D', sysdate - 2, 'info' from dual union all
 19  select 6, 'D', sysdate - 2, 'info' from dual union all
 20  select 7, 'A', sysdate - 1, 'info' from dual union all
 21  select 7, 'A', sysdate - 1, 'info' from dual union all
 22  select 7, 'A', sysdate - 1, 'info' from dual union all
 23  select 7, 'A', sysdate - 1, 'info' from dual
 24  /

Table created.

您的查询不起作用:有多余的右括号。如果我删除它们,它仍然不起作用,因为您正在将日期与 varchar2 进行比较:

SQL> select object_name,
  2         event_number,
  3         count(*),
  4         event_supplementary_info
  5    from event_table
  6   where event_time between to_char(sysdate -7, 'YYYY-MM-DD')
  7                        and to_char(sysdate , 'YYYY-MM-DD')
  8  group by object_name, event_number, event_supplementary_info
  9  /

no rows selected

所以我的基本查询是我将日期与日期进行比较的查询:

SQL> select object_name
  2       , event_number
  3       , count(*)
  4       , event_supplementary_info
  5    from event_table
  6   where event_time between sysdate -7 and sysdate
  7   group by object_name
  8       , event_number
  9       , event_supplementary_info
 10   order by object_name
 11       , event_number
 12  /

O EVENT_NUMBER   COUNT(*) EVEN
- ------------ ---------- ----
A            1          1 info
A            2          1 info
A            3          2 info
A            5          1 info
A            7          4 info
B            1          2 info
B            2          2 info
C            4          5 info
D            4          1 info
D            6          2 info

10 rows selected.

我解释了您的问题,即您想要一条包含每个事件总数的记录对象名称。对于此示例数据,您需要 4 个额外记录,分别用于对象 A、B、C 和 D。为了实现这一点,我仅在 object_name 上添加了另一个分组集。为了清楚起见,我将 trunc(event_time) 添加到现有分组集中。

SQL> select object_name
  2       , event_number
  3       , count(*)
  4       , event_supplementary_info
  5       , trunc(event_time)
  6    from event_table
  7   where event_time between sysdate -7 and sysdate
  8   group by grouping sets
  9         ( ( object_name
 10           , event_number
 11           , event_supplementary_info
 12           , trunc(event_time)
 13           )
 14         , ( object_name )
 15         )
 16   order by object_name
 17       , event_number
 18  /

O EVENT_NUMBER   COUNT(*) EVEN TRUNC(EVENT_TIME)
- ------------ ---------- ---- -------------------
A            1          1 info 15-08-2010 00:00:00
A            2          1 info 16-08-2010 00:00:00
A            3          2 info 17-08-2010 00:00:00
A            5          1 info 19-08-2010 00:00:00
A            7          4 info 21-08-2010 00:00:00
A                       9
B            1          2 info 15-08-2010 00:00:00
B            2          2 info 16-08-2010 00:00:00
B                       4
C            4          5 info 18-08-2010 00:00:00
C                       5
D            4          1 info 18-08-2010 00:00:00
D            6          2 info 20-08-2010 00:00:00
D                       3

14 rows selected.

编写此类查询的简短方法是将分组集转换为汇总:

SQL> select object_name
  2       , event_number
  3       , count(*)
  4       , event_supplementary_info
  5       , trunc(event_time)
  6    from event_table
  7   where event_time between sysdate -7 and sysdate
  8   group by object_name
  9       , rollup ((event_number,event_supplementary_info,trunc(event_time)))
 10   order by object_name
 11       , event_number
 12  /

O EVENT_NUMBER   COUNT(*) EVEN TRUNC(EVENT_TIME)
- ------------ ---------- ---- -------------------
A            1          1 info 15-08-2010 00:00:00
A            2          1 info 16-08-2010 00:00:00
A            3          2 info 17-08-2010 00:00:00
A            5          1 info 19-08-2010 00:00:00
A            7          4 info 21-08-2010 00:00:00
A                       9
B            1          2 info 15-08-2010 00:00:00
B            2          2 info 16-08-2010 00:00:00
B                       4
C            4          5 info 18-08-2010 00:00:00
C                       5
D            4          1 info 18-08-2010 00:00:00
D            6          2 info 20-08-2010 00:00:00
D                       3

14 rows selected.

问候,
抢。

Here is a try using this sample data. For next time please provide some sample data yourself.

SQL> create table event_table (event_number,object_name,event_time,event_supplementary_info)
  2  as
  3  select 1, 'A', sysdate - 7, 'info' from dual union all
  4  select 1, 'B', sysdate - 7, 'info' from dual union all
  5  select 1, 'B', sysdate - 7, 'info' from dual union all
  6  select 2, 'A', sysdate - 6, 'info' from dual union all
  7  select 2, 'B', sysdate - 6, 'info' from dual union all
  8  select 2, 'B', sysdate - 6, 'info' from dual union all
  9  select 3, 'A', sysdate - 5, 'info' from dual union all
 10  select 3, 'A', sysdate - 5, 'info' from dual union all
 11  select 4, 'C', sysdate - 4, 'info' from dual union all
 12  select 4, 'C', sysdate - 4, 'info' from dual union all
 13  select 4, 'C', sysdate - 4, 'info' from dual union all
 14  select 4, 'C', sysdate - 4, 'info' from dual union all
 15  select 4, 'C', sysdate - 4, 'info' from dual union all
 16  select 4, 'D', sysdate - 4, 'info' from dual union all
 17  select 5, 'A', sysdate - 3, 'info' from dual union all
 18  select 6, 'D', sysdate - 2, 'info' from dual union all
 19  select 6, 'D', sysdate - 2, 'info' from dual union all
 20  select 7, 'A', sysdate - 1, 'info' from dual union all
 21  select 7, 'A', sysdate - 1, 'info' from dual union all
 22  select 7, 'A', sysdate - 1, 'info' from dual union all
 23  select 7, 'A', sysdate - 1, 'info' from dual
 24  /

Table created.

Your query doesn't work: there are superfluous right brackets. If I remove them, it still doesn't work because you are comparing dates with varchar2's:

SQL> select object_name,
  2         event_number,
  3         count(*),
  4         event_supplementary_info
  5    from event_table
  6   where event_time between to_char(sysdate -7, 'YYYY-MM-DD')
  7                        and to_char(sysdate , 'YYYY-MM-DD')
  8  group by object_name, event_number, event_supplementary_info
  9  /

no rows selected

So my base query is this one where I compare the dates with dates:

SQL> select object_name
  2       , event_number
  3       , count(*)
  4       , event_supplementary_info
  5    from event_table
  6   where event_time between sysdate -7 and sysdate
  7   group by object_name
  8       , event_number
  9       , event_supplementary_info
 10   order by object_name
 11       , event_number
 12  /

O EVENT_NUMBER   COUNT(*) EVEN
- ------------ ---------- ----
A            1          1 info
A            2          1 info
A            3          2 info
A            5          1 info
A            7          4 info
B            1          2 info
B            2          2 info
C            4          5 info
D            4          1 info
D            6          2 info

10 rows selected.

I interpreted your question that you want a record with the total number of events per object_name. With this sample data, you need 4 extra records, for objects A, B, C and D. To achieve that, I added another grouping set on just object_name. And I included trunc(event_time) to the existing grouping set for clarity.

SQL> select object_name
  2       , event_number
  3       , count(*)
  4       , event_supplementary_info
  5       , trunc(event_time)
  6    from event_table
  7   where event_time between sysdate -7 and sysdate
  8   group by grouping sets
  9         ( ( object_name
 10           , event_number
 11           , event_supplementary_info
 12           , trunc(event_time)
 13           )
 14         , ( object_name )
 15         )
 16   order by object_name
 17       , event_number
 18  /

O EVENT_NUMBER   COUNT(*) EVEN TRUNC(EVENT_TIME)
- ------------ ---------- ---- -------------------
A            1          1 info 15-08-2010 00:00:00
A            2          1 info 16-08-2010 00:00:00
A            3          2 info 17-08-2010 00:00:00
A            5          1 info 19-08-2010 00:00:00
A            7          4 info 21-08-2010 00:00:00
A                       9
B            1          2 info 15-08-2010 00:00:00
B            2          2 info 16-08-2010 00:00:00
B                       4
C            4          5 info 18-08-2010 00:00:00
C                       5
D            4          1 info 18-08-2010 00:00:00
D            6          2 info 20-08-2010 00:00:00
D                       3

14 rows selected.

The short way to write such a query, is to convert the grouping sets to a rollup:

SQL> select object_name
  2       , event_number
  3       , count(*)
  4       , event_supplementary_info
  5       , trunc(event_time)
  6    from event_table
  7   where event_time between sysdate -7 and sysdate
  8   group by object_name
  9       , rollup ((event_number,event_supplementary_info,trunc(event_time)))
 10   order by object_name
 11       , event_number
 12  /

O EVENT_NUMBER   COUNT(*) EVEN TRUNC(EVENT_TIME)
- ------------ ---------- ---- -------------------
A            1          1 info 15-08-2010 00:00:00
A            2          1 info 16-08-2010 00:00:00
A            3          2 info 17-08-2010 00:00:00
A            5          1 info 19-08-2010 00:00:00
A            7          4 info 21-08-2010 00:00:00
A                       9
B            1          2 info 15-08-2010 00:00:00
B            2          2 info 16-08-2010 00:00:00
B                       4
C            4          5 info 18-08-2010 00:00:00
C                       5
D            4          1 info 18-08-2010 00:00:00
D            6          2 info 20-08-2010 00:00:00
D                       3

14 rows selected.

Regards,
Rob.

梦巷 2024-09-22 09:47:40

或者也许这就是您正在寻找的: http://psoug.org/reference/rollup.html< /a>?

Or maybe this is what you are looking for: http://psoug.org/reference/rollup.html ?

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