일일 작업TYPE 시간대별 수량 뽑는 쿼리인데 어케어케 원하는대로 정보는 뽑아집니다 혹시 이걸 더 효울적으로 할 수 있는 방법이 있나 문의 드려요
특정 시간대별 (07시부터 19시까지).. 1시간 단위로(정확히는 59분 단위) 작업TYPE별 물량을 뽑는건데
전 하드코딩한 시간을 가로를 세로로 UNPIVOT 시키는고 작업별 물량을 따로 만들어서 해당 작업의 시간(07,08....)과 하드코딩한 시간을 매칭해서 작성했는데요
시간대를 유동적으로 할 수 있는 방법이 있을꺼 같기도 하고 또 전체적으로 더 효율적으로 작성 할 방법이 있나 문의 드립니다.
시간 DS LD MI MO GI GO 합계
07:00 ~ 07:59 0 0 0 0 38 69 107
08:00 ~ 08:59 0 1 0 1 73 99 174
09:00 ~ 09:59 12 0 3 3 130 96 244
10:00 ~ 10:59 9 1 3 1 152 112 278
11:00 ~ 11:59 0 0 1 1 183 158 343
12:00 ~ 12:59 0 0 0 0 201 158 359
13:00 ~ 13:59 5 0 1 1 169 120 296
14:00 ~ 14:59 6 1 1 2 147 121 278
15:00 ~ 15:59 4 4 0 0 151 102 261
16:00 ~ 16:59 1 3 1 0 148 85 238
17:00 ~ 17:59 0 5 0 0 114 77 196
18:00 ~ 18:59 0 0 0 0 40 37 77
총합계 37 15 10 9 1546 1234 2851
-----------------------------------------------------------
SELECT DECODE(LENGTH(TIMES), 13, TIMES, '총합계') AS TIMES, SUM(job_type.DS) AS "DS (양하)", SUM(job_type.LD) AS "LD (선적)", SUM(job_type.MI) AS "MI (구입)", SUM(job_type.MO) AS "MO (구출)", SUM(job_type.GI) AS "GI (반입)", SUM(job_type.GO) AS "GO (반출)", SUM(job_type.DS) + SUM(job_type.LD) + SUM(job_type.MI) + SUM(job_type.MO) + SUM(job_type.GI) + SUM(job_type.GO) AS "합계" FROM ( SELECT SLOT, TIMES FROM ( SELECT '07:00 ~ 07:59' AS T07, '08:00 ~ 08:59' AS T08, '09:00 ~ 09:59' AS T09, '10:00 ~ 10:59' AS T10, '11:00 ~ 11:59' AS T11, '12:00 ~ 12:59' AS T12, '13:00 ~ 13:59' AS T13, '14:00 ~ 14:59' AS T14, '15:00 ~ 15:59' AS T15, '16:00 ~ 16:59' AS T16, '17:00 ~ 17:59' AS T17, '18:00 ~ 18:59' AS T18 FROM DUAL ) UNPIVOT ( TIMES FOR SLOT IN(T07,T08,T09,T10,T11,T12,T13,T14,T15,T16,T17,T18) ) ) slot, ( SELECT SLOT, SUM(DS) AS DS, SUM(LD) AS LD, SUM(MI) AS MI, SUM(MO) AS MO, SUM(GI) AS GI, SUM(GO) AS GO FROM ( SELECT SUBSTR(MCH_RMT_STARTTIME, 9, 4) AS SLOT, SUM(DECODE(MCH_RMT_JOBTYPE, 'DS', 1, 0)) AS DS, SUM(DECODE(MCH_RMT_JOBTYPE, 'LD', 1, 0)) AS LD, SUM(DECODE(MCH_RMT_JOBTYPE, 'MI', 1, 0)) AS MI, SUM(DECODE(MCH_RMT_JOBTYPE, 'MO', 1, 0)) AS MO, SUM(DECODE(MCH_RMT_JOBTYPE, 'GI', 1, 0)) AS GI, SUM(DECODE(MCH_RMT_JOBTYPE, 'GO', 1, 0)) AS GO FROM MCH_REMOTE WHERE MCH_RMT_STARTTIME >= '202204150700' AND MCH_RMT_STARTTIME <= '202204151900' AND MCH_RMT_JOBTYPE NOT IN ('AH', 'RH', 'GC', 'MV') GROUP BY SUBSTR(MCH_RMT_STARTTIME, 9, 4), MCH_RMT_JOBTYPE ORDER BY MCH_RMT_STARTTIME ASC ) GROUP BY SLOT ORDER BY SLOT ) job_type WHERE SUBSTR(slot.slot,2,2) = SUBSTR(job_type.slot,1,2) GROUP BY ROLLUP(TIMES) ORDER BY TIMES
SELECT DECODE(GROUPING(SUBSTR(mch_rmt_starttime, 9, 2)), 1, '총합계', SUBSTR(mch_rmt_starttime, 9, 2) || ':00 ~ ' || SUBSTR(mch_rmt_starttime, 9, 2) || ':59') times , COUNT(DECODE(mch_rmt_jobtype, 'DS', 1)) "DS (양하)" , COUNT(DECODE(mch_rmt_jobtype, 'LD', 1)) "LD (선적)" , COUNT(DECODE(mch_rmt_jobtype, 'MI', 1)) "MI (구입)" , COUNT(DECODE(mch_rmt_jobtype, 'MO', 1)) "MO (구출)" , COUNT(DECODE(mch_rmt_jobtype, 'GI', 1)) "GI (반입)" , COUNT(DECODE(mch_rmt_jobtype, 'GO', 1)) "GO (반출)" , COUNT(*) 합계 FROM mch_remote WHERE mch_rmt_starttime >= '202204150700' AND mch_rmt_starttime < '202204151900' AND mch_rmt_jobtype IN ('DS', 'LD', 'MI', 'MO', 'GI', 'GO') GROUP BY ROLLUP(SUBSTR(mch_rmt_starttime, 9, 2)) ;