안녕하세요 그룹바이 관련해서 질문좀 드리려구요~ 0 3 1,077

by 겨울이불 [SQL Query] group by [2020.07.29 10:39:54]


with AA as
(select '20200601' as start_dt, '20200601' as aply_dt, 2749 as cnt, 1 as work_type
 from dual
 union all
 select '20200602' as start_dt, '20200602' as aply_dt,  3342as cnt, 1 as work_type
 from dual
  union all
 select '20200603' as start_dt, '20200603' as aply_dt, 5434 as cnt, 0 as work_type
 from dual
  union all
 select '20200604' as start_dt, '20200604' as aply_dt, 3445 as cnt, 0 as work_type
 from dual
  union all
 select '20200605' as start_dt, '20200605' as aply_dt, 2322 as cnt, 1 as work_type
 from dual
  union all
 select '20200606' as start_dt, '20200606' as aply_dt, 4324 as cnt, 1 as work_type
 from dual
 union all
  select '20200607' as start_dt, '20200607' as aply_dt, 3336 as cnt, 2 as work_type
 from dual
  union all
  select '20200608' as start_dt, '20200608' as aply_dt, 4543 as cnt, 0 as work_type
 from dual
  union all
  select '20200609' as start_dt, '20200609' as aply_dt, 1233 as cnt, 1 as work_type
 from dual
   union all
  select '20200610' as start_dt, '20200610' as insr_end_dt, 3232 as cnt, 1 as work_type
 from dual
 )
  select * from AA;

 

 /*
 결과

START_DT APLY_DT  CNT
20200601         20200601                                2749
20200602         20200602                                3342
20200605         20200603/20200604/20200605     5434+3445+2322
20200606         20200606                                4324
20200609   20200607/20200608/20200609     3336+4543+1233
20200610       20200610 3232
     


work_type = 1이 아닌경우 다음날짜의 work_type값이 1이 나올때까지 모두 그룹화해서 보일수있게끔 하고싶습니다.
혹시 이게 가능할까요? 제지식으로는 찾기가힘들어서요..
(참고 : work_type =1(평일), 0,2(휴일) )

*/

by 우리집아찌 [2020.07.29 11:04:34]
with AA as
(select '20200601' as start_dt, '20200601' as aply_dt, 2749 as cnt, 1 as work_type
 from dual
 union all
 select '20200602' as start_dt, '20200602' as aply_dt,  3342as cnt, 1 as work_type
 from dual
  union all
 select '20200603' as start_dt, '20200603' as aply_dt, 5434 as cnt, 0 as work_type
 from dual
  union all
 select '20200604' as start_dt, '20200604' as aply_dt, 3445 as cnt, 0 as work_type
 from dual
  union all
 select '20200605' as start_dt, '20200605' as aply_dt, 2322 as cnt, 1 as work_type
 from dual
  union all
 select '20200606' as start_dt, '20200606' as aply_dt, 4324 as cnt, 1 as work_type
 from dual
 union all
  select '20200607' as start_dt, '20200607' as aply_dt, 3336 as cnt, 2 as work_type
 from dual
  union all
  select '20200608' as start_dt, '20200608' as aply_dt, 4543 as cnt, 0 as work_type
 from dual
  union all
  select '20200609' as start_dt, '20200609' as aply_dt, 1233 as cnt, 1 as work_type
 from dual
   union all
  select '20200610' as start_dt, '20200610' as insr_end_dt, 3232 as cnt, 1 as work_type
 from dual
 )
  SELECT MAX(START_DT) START_DT
       , LISTAGG(APLY_DT, '/') WITHIN GROUP (ORDER BY APLY_DT) AS APLY_DT
       , LISTAGG(CNT, '+') WITHIN GROUP (ORDER BY APLY_DT)     AS CNT
   FROM ( SELECT A.*
                 ,  LAST_VALUE(GRP) IGNORE NULLS OVER(ORDER BY START_DT DESC) GB
                  FROM (  SELECT AA.* 
                             , START_DT - CASE WHEN WORK_TYPE = 1 THEN 0 END GRP
                         FROM AA 
                       ) A
           )  GROUP BY GB

 


by 마농 [2020.07.29 11:10:18]
WITH aa AS
(
SELECT '20200601' start_dt, '20200601' aply_dt, 2749 cnt, 1 work_type FROM dual
UNION ALL SELECT '20200602', '20200602', 3342, 1 FROM dual
UNION ALL SELECT '20200603', '20200603', 5434, 0 FROM dual
UNION ALL SELECT '20200604', '20200604', 3445, 0 FROM dual
UNION ALL SELECT '20200605', '20200605', 2322, 1 FROM dual
UNION ALL SELECT '20200606', '20200606', 4324, 1 FROM dual
UNION ALL SELECT '20200607', '20200607', 3336, 2 FROM dual
UNION ALL SELECT '20200608', '20200608', 4543, 0 FROM dual
UNION ALL SELECT '20200609', '20200609', 1233, 1 FROM dual
UNION ALL SELECT '20200610', '20200610', 3232, 1 FROM dual
)
SELECT MAX(start_dt) start_dt
     , LISTAGG(aply_dt, '/') WITHIN GROUP(ORDER BY aply_dt) aply_dt
     , SUM(cnt) cnt
  FROM (SELECT start_dt, aply_dt, cnt, work_type
             , COUNT(DECODE(work_type, 1, 1)) OVER(ORDER BY start_dt, aply_dt
               ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) gb
          FROM aa
        )
 GROUP BY gb
;

 


by 겨울이불 [2020.07.30 08:48:00]

너무 감사합니다.~~

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입