2015-02-05 3 views
0

나는이 다음과 같은 DB2 테이블 이름이 그 사고 난 결과의 측면에서 얻으려고 무엇DB2의 월별 그룹과 여러으로 계산 곳에 절을

ticketid  Createdon  Severity 
1   2012-01-01 1 
2   2012-01-10 2 
3   2012-01-15 2 
4   2012-01-20 3 
5   2012-01-29 3 
6   2012-02-06 3 
7   2012-02-15 3 
8   2012-02-20 3 
9   2012-02-20 4 
10   2012-02-21 3 

:

Month  Sev1 Sev2 Sev3 Sev4 
==================================== 
2012-01  1  2  2  0 
2012-02  0  0  4  1 

을 지금까지를 내가 ...

012,351,641

select to_char(INCIDENT.CREATEDON, 'YYYY-MM') as month, 
count(case when severity = 1 then 1 else 0 end) as SEV1, 
count(case when severity = 2 then 1 else 0 end) as SEV2 
from INCIDENT group by to_char(INCIDENT.CREATEDON,'YYYY-MM') 

을하지만 모두 열이 나에게 모든 사건을 제공 :이 있습니다

Month  Sev1 Sev2 Sev3 Sev4 
==================================== 
2012-01  11  11  11  11 
2012-02  16  16  16  16 

어떤 개념이 좋지 않습니까? 나는이 일을 할 수 있다고 생각하지만이 주위에 내 머리를 감쌀 수 없다 ...

답변

1

나는 그것을 얻었다! 물론 내 질문을 게시 한 후 그것을 알아 낸 ....

select to_char(INCIDENT.CREATEDON, 'YYYY-MM') as month, 
sum(case when severity = 1 then 1 else 0 end) as sev1, 
sum(case when severity = 2 then 1 else 0 end) as sev2, 
sum(case when severity = 3 then 1 else 0 end) as sev3, 
sum(case when severity = 4 then 1 else 0 end) as sev4 
from INCIDENT 
where (createdon >= current_timestamp - (minute(current_timestamp) minute) - (hour(current_timestamp) -1) hours - (day(current_date) - 1) days - 13 month) group by to_char(INCIDENT.CREATEDON,'YYYY-MM')