날짜의 시간별 합계 질문드립니다 0 2 2,606

by 김용한 [SQL Query] [2012.12.24 19:40:47]


RPTDATE failcnt
2012-12-04 04:00:00 600
2012-12-04 04:10:00 562
2012-12-04 04:20:00 631
2012-12-04 04:30:00 663
2012-12-04 04:40:00 644
2012-12-04 04:50:00 673
2012-12-04 05:00:00 679
2012-12-04 05:10:00 617
2012-12-04 05:20:00 633
2012-12-04 05:30:00 639
2012-12-04 05:40:00 691
2012-12-04 05:50:00 606
2012-12-04 06:00:00 603
2012-12-04 06:10:00 651
2012-12-04 06:20:00 628
2012-12-04 06:30:00 593
2012-12-04 06:40:00 552
2012-12-04 06:50:00 551


==>


RPTDATE FAILCNT
2012-12-04 04:00:00 3773
2012-12-04 05:00:00 3865
2012-12-04 06:00:00 3578


이렇게 나오게하는쿼리가

WITH T AS
(
SELECT TO_DATE('2012-12-04 04:00:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '600' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:10:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '562' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:20:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '631' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:30:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '663' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:40:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '644' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:50:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '673' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:00:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '679' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:10:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '617' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:20:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '633' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:30:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '639' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:40:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '691' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:50:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '606' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:00:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '603' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:10:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '651' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:20:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '628' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:30:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '593' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:40:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '552' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:50:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '551' FAILCNT FROM DUAL
)
SELECT TO_CHAR(RPTDATE,'YYYY-MM-DD HH') RPTDATE
, SUM(FAILCNT) FAILCNT
  FROM T
 GROUP BY TO_CHAR(RPTDATE,'YYYY-MM-DD HH')
 ORDER BY RPTDATE

이렇게하면될까요?

오라클이안깔려있어서 쿼리테스트를못해보겠네요...

by Oracler [2012.12.24 20:22:16]
김용한님 쿼리로 하니까 아래와 같은 결과가 나오네요.

RPTDATE          FAILCNT
------------- ----------
2012-12-04 04       3773
2012-12-04 05       3865
2012-12-04 06       3578


조금 다르게 나와서 제 나름대로 작성해 봤습니다.
그럼, 도움이 되셨길...


WITH T AS
(
SELECT TO_DATE('2012-12-04 04:00:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '600' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:10:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '562' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:20:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '631' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:30:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '663' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:40:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '644' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 04:50:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '673' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:00:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '679' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:10:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '617' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:20:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '633' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:30:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '639' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:40:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '691' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 05:50:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '606' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:00:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '603' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:10:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '651' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:20:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '628' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:30:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '593' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:40:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '552' FAILCNT FROM DUAL UNION ALL
SELECT TO_DATE('2012-12-04 06:50:00','YYYY-MM-DD HH24:MI:SS') RPTDATE, '551' FAILCNT FROM DUAL
)
SELECT
  TO_CHAR(TRUNC(rptdate, 'HH24'), 'yyyy-mm-dd hh24:mi:ss') AS rptdate
 , SUM(failcnt) AS failcnt
FROM
  t
GROUP BY
  TRUNC(rptdate, 'HH24')
ORDER BY
  1
;

by 김용한 [2012.12.24 21:02:52]

 감사드립니다 ~ 저런방법도있었군요 ^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입