简体   繁体   English

Oracle按日期和数字查询条形图

[英]Oracle query by date and number for bar chart

I have this table which I use to store 4 types of messages: info, warn, alarm and error. 我有此表,用于存储4种类型的消息:信息,警告,警报和错误。

CREATE TABLE EVENTS(
  EVENTID INTEGER NOT NULL,
  SOURCE VARCHAR2(50 ),
  TYPE VARCHAR2(50 ),
  EVENT_DATE DATE,
  DESCRIPTION VARCHAR2(100 )
)
/

I need to display the events by type and day in a bar chart 15 days into the past for example: 例如,我需要在过去15天的条形图中按类型和日期显示事件: 在此处输入图片说明

Can you help me for this Oracle query? 您可以帮助我进行此Oracle查询吗?

Example output from query: 查询的示例输出:

|11-12-2015 | 12 | 22 | 11 |
|12-12-2015 | 32 | 12 | 14 |
|13-12-2015 | 12 | 22 | 11 |
|14-12-2015 | 12 | 22 | 11 |

You can just use conditional aggregation: 您可以只使用条件聚合:

select event_date,
       sum(case when type = 'Error' then 1 else 0 end) as Error,
       sum(case when type = 'Warn' then 1 else 0 end) as Warn,
       sum(case when type = 'Info' then 1 else 0 end) as Info
from events e
where event_date >= trunc(sysdate) - 15
group by event_date
order by event_date;

Caveat: if the field called event_date actually has a time component, then you should use trunc(event_date) in the select and group by . 注意:如果名为event_date的字段实际上具有时间成分,则应在selectgroup by使用trunc(event_date)

Variant A: line by line You want the number (=COUNT(*)) of eg error (=TYPE) events of a specific source at a specific event_date. 变体A:逐行您希望在特定event_date特定来源的错误(= TYPE)事件的数量(= COUNT(*))。 So the triple ( source, type, event_date) build a group and you want to know, how many events are in this group. 因此,三元组(源,类型,event_date)建立了一个组,您想知道该组中有多少个事件。

Select source, type, event_date, count(*) 
from events 
group by source, type, event_date;

With this the results would be 有了这个结果将是

event_date  source  type   count
11-12-2015  server  error  12
11-12-2015  server  info   22
11-12-2015  server  warn   11
12-12-2015  server  error  32
12-12-2015  server  info   12
12-12-2015  server  warn   14

Variant B: side by side 变体B:并排

Select event_date, source, sum(error), sum(info), sum(warn)
From (select event_date, source, 
decode(Type, 'error', 1, 0) as error,
decode(Type, 'info', 1, 0) as info,
decode(Type, 'warn', 1, 0) as warn
From events)
Group by event_date, source;

You should get: 您应该得到:

event_date  source  error  warn  info
11-12-2015  server   12    22    11
12-12-2015  server   32    12    14

decode is some kind of if-then-else. decode是一种if-then-else。

Optional: only 15 days back As you asked. 可选:仅在15天后根据您的要求。

Simple add where clause to the (inner) select, like where event_date >= sysdate - 15 简单地在(内部)选择中添加where子句,例如where event_date >= sysdate - 15

Select source, type, event_date, count(*) 
from events 
where event_date >= sysdate - 15
group by source, type, event_date;

or for b) 或b)

Select event_date, source, sum(error), sum(info), sum(warn)
From (select event_date, source, 
decode(Type, 'error', 1, 0) as error,
decode(Type, 'info', 1, 0) as info,
decode(Type, 'warn', 1, 0) as warn
From events
where event_date >= sysdate - 15)
Group by event_date, source;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM