我需要有关创建查询的帮助。
目前它看起来像这样
SELECT
COUNT(*) AS ALERTS,
EXTRACT (YEAR FROM DATE) AS YEAR,
EXTRACT (MONTH FROM DATE) AS MONTH,
EXTRACT (DAY FROM DATE) AS DAY,
HOST,
ALERTLEVEL
FROM
ALERTS
WHERE
EXTRACT (DAY FROM DATE) > EXTRACT (DAY FROM CURRENT_DATE) -7
AND
EXTRACT (MONTH FROM DATE) >= EXTRACT (MONTH FROM CURRENT_DATE)
AND
HOST = 'HOST'
GROUP BY
YEAR,MONTH,DAY,HOST,ALERTLEVEL
ORDER BY
YEAR ASC,MONTH ASC,DAY ASC, ALERTLEVEL ASC
结果:
ALERTS YEAR MONTH DAY HOST ALERTLEVEL
5 2013 9 5 HOST 1
64 2013 9 5 HOST 2
11 2013 9 5 HOST 3
15 2013 9 5 HOST 4
16 2013 9 8 HOST 2
2 2013 9 9 HOST 1
我想为alertlevel而不是一列提供4列,并为每个警报级别每天汇总列警报。
期望的结果
YEAR MONTH DAY HOST ALERTLEVEL1 ALERTLEVEL2 ALERTLEVEL3 ALERTLEVEL4
2013 9 5 HOST 5 64 11 15
2013 9 8 HOST 0 16 0 0
2013 9 9 HOST 2 0 0 0
这可能吗?
非常感谢任何帮助!!
答案 0 :(得分:4)
SELECT
COUNT(*) AS ALERTS,
EXTRACT (YEAR FROM DATE) AS YEAR,
EXTRACT (MONTH FROM DATE) AS MONTH,
EXTRACT (DAY FROM DATE) AS DAY,
HOST,
sum(case when ALERTLEVEL = 1 then 1 else 0 end) as ALERTLEVEL1,
sum(case when ALERTLEVEL = 2 then 1 else 0 end) as ALERTLEVEL2,
sum(case when ALERTLEVEL = 3 then 1 else 0 end) as ALERTLEVEL3,
sum(case when ALERTLEVEL = 4 then 1 else 0 end) as ALERTLEVEL4
FROM
ALERTS
WHERE
EXTRACT (DAY FROM DATE) > EXTRACT (DAY FROM CURRENT_DATE) -7
AND
EXTRACT (MONTH FROM DATE) >= EXTRACT (MONTH FROM CURRENT_DATE)
AND
HOST = 'HOST'
GROUP BY
YEAR,MONTH,DAY,HOST
ORDER BY
YEAR ASC,MONTH ASC,DAY ASC