按日期分组事件

时间:2016-01-26 21:41:17

标签: sql oracle oracle11g

我有这个用于事件的Oracle表

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

我使用此SQL查询按类型获取数据:

SELECT EVENT_DATE, SUM(CASE WHEN TYPE = 'Error' THEN 1 ELSE 0 END) AS ERROR, 
SUM(CASE WHEN TYPE = 'Warning' THEN 1 ELSE 0 END) AS WARN, 
SUM(CASE WHEN TYPE = 'Info' THEN 1 ELSE 0 END) AS INFO, 
SUM(CASE WHEN TYPE = 'Critical' THEN 1 ELSE 0 END) AS CRITICAL
FROM EVENT E WHERE EVENT_DATE >= TRUNC(SYSDATE) - 7 
GROUP BY EVENT_DATE 
HAVING COUNT(CASE WHEN TYPE IN ('Info', 'Warning', 'Error', 'Critical') THEN 1 END) > 0
ORDER BY EVENT_DATE

我如何才能获得一天的结果?我想让所有活动连续7天分开。我应该有7行数据结果

2 个答案:

答案 0 :(得分:3)

当然David Bachmann Jeppesen的回答是正确的,但您的查询可以简化。

HAVING条件可以在WHERE中应用,在聚合之前减少行数

SELECT trunc(EVENT_DATE) event_date,
   SUM(CASE WHEN TYPE = 'Error' THEN 1 ELSE 0 END) AS ERROR,
   SUM(CASE WHEN TYPE = 'Warning' THEN 1 ELSE 0 END) AS WARN,
   SUM(CASE WHEN TYPE = 'Info' THEN 1 ELSE 0 END) AS INFO,
   SUM(CASE WHEN TYPE = 'Critical' THEN 1 ELSE 0 END) AS CRITICAL
FROM EVENT E
WHERE EVENT_DATE >= TRUNC(SYSDATE) - 7
  AND TYPE IN ('Info', 'Warning', 'Error', 'Critical')
GROUP BY trunc(EVENT_DATE)
ORDER BY trunc(EVENT_DATE)

答案 1 :(得分:2)

  render: function() {
    return <input type="text"
      onChange     = {this.onChange}
      defaultValue = {this.text}
      data-what    = {this.text} />;
  }