限制返回数据7天

时间:2016-01-26 19:02:03

标签: sql oracle oracle11g

我有这个用于存储事件的表

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

我使用此查询来获取所有类型事件的结果

dbRequest.setPreparedStatement("SELECT "
            + " COUNT(*) \"ALL\","
            + " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"CRITICAL\", "
            + " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"INFO\", "
            + " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"WARN\", "
            + " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"ERROR\" "
            + " FROM "
            + " EVENT");

如何修改查询以仅在过去7天内获取行? 此外,由于某种原因,查询不适用于低资本数据。

1 个答案:

答案 0 :(得分:2)

dbRequest.setPreparedStatement("SELECT "
            + " COUNT(*) \"ALL\","
            + " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"CRITICAL\", "
            + " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"INFO\", "
            + " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"WARN\", "
            + " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"ERROR\" "
            + " FROM "
            + " EVENT"
            + " WHERE EVENT_DATE >= TRUNC(SYSDATE)-7");