안녕하세요. 해당 쿼리에서 데이터 조회하는데 원하는 데이터가 조회가 되지 않아서 여쭤봅니다. 현재 데이터에서 조건을 0부터 ~ 365 사이의 데이터만(1년미만) 출력하고 조건이 365 ~ 729 의 데이터만(1년이상....) 출력을 하려고 합니다. 어떻게 조건을 주면은 좋을지 답변을 주시면 감사하겠습니다. WITH tech AS ( SELECT 'AAA' cd, '001' itsqf, '20130101' s_day, '20131231' e_day FROM dual UNION ALL SELECT 'AAA', '001', '20130601', '20160831' FROM dual UNION ALL SELECT 'AAA', '001', '20140101', '20141231' FROM dual UNION ALL SELECT 'AAA', '001', '20140401', '20140531' FROM dual UNION ALL SELECT 'AAA', '001', '20140801', '20141130' FROM dual UNION ALL SELECT 'AAA', '001', '20150101', '20151231' FROM dual UNION ALL SELECT 'AAA', '001', '20160101', '20160331' FROM dual UNION ALL SELECT 'AAA', '001', '20200302', '20200622' FROM dual UNION ALL SELECT 'AAA', '001', '20021223', '20070930' FROM dual UNION ALL SELECT 'AAA', '006', '20060615', '20060830' FROM dual UNION ALL SELECT 'AAA', '003', '20140613', '20141130' FROM dual UNION ALL SELECT 'AAA', '004', '20161101', '20161201' FROM dual UNION ALL SELECT 'AAA', '005', '20161201', '20190702' FROM dual UNION ALL SELECT 'BBB', '001', '20100201', '20110131' FROM dual UNION ALL SELECT 'BBB', '001', '20100201', '20110708' FROM dual UNION ALL SELECT 'BBB', '001', '20101220', '20110901' FROM dual UNION ALL SELECT 'BBB', '001', '20100301', '20110228' FROM dual UNION ALL SELECT 'BBB', '001', '20110101', '20110901' FROM dual UNION ALL SELECT 'BBB', '002', '20080406', '20080721' FROM dual UNION ALL SELECT 'BBB', '002', '20080912', '20081111' FROM dual UNION ALL SELECT 'BBB', '002', '20081101', '20090721' FROM dual UNION ALL SELECT 'BBB', '002', '20081117', '20081230' FROM dual UNION ALL SELECT 'BBB', '002', '20081117', '20090721' FROM dual UNION ALL SELECT 'CCC', '005', '20140401', '20140531' FROM dual UNION ALL SELECT 'CCC', '005', '20140801', '20141130' FROM dual UNION ALL SELECT 'CCC', '005', '20150101', '20151231' FROM dual UNION ALL SELECT 'CCC', '005', '20160101', '20160331' FROM dual UNION ALL SELECT 'CCC', '005', '20200302', '20200622' FROM dual UNION ALL SELECT 'DDD', '002', '19990104', '19991130' FROM dual UNION ALL SELECT 'DDD', '005', '20070702', '20080608' FROM dual UNION ALL SELECT 'DDD', '005', '20081208', '20090630' FROM dual UNION ALL SELECT 'DDD', '005', '20130701', '20140630' FROM dual ) SELECT * from( SELECT cd, SUM(DECODE(itsqf,'001', to_date(e) - to_date(s))) AA , SUM(DECODE(itsqf,'002', to_date(e) - to_date(s))) AB , SUM(DECODE(itsqf,'003', to_date(e) - to_date(s))) AC , SUM(DECODE(itsqf,'004', to_date(e) - to_date(s))) AD , SUM(DECODE(itsqf,'005', to_date(e) - to_date(s))) AE , SUM(DECODE(itsqf,'006', to_date(e) - to_date(s))) AF FROM (SELECT cd, itsqf , MIN(s_day) s , MAX(e_day) e FROM (SELECT cd, itsqf, s_day, e_day , SUM(flag) OVER(PARTITION BY cd, itsqf ORDER BY s_day, e_day) grp FROM (SELECT cd, itsqf, s_day, e_day , CASE WHEN s_day <= MAX(e_day) OVER(PARTITION BY cd, itsqf ORDER BY s_day, e_day ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) THEN 0 ELSE 1 END flag FROM tech ) ) GROUP BY cd, itsqf, grp ) GROUP BY cd )
1 열은 가변으로 조정할 수 없습니다.
2. 일수계산시 +1 을 해줘야 합니다.
3. s_day, e_day 가 Date 타입이라고 가정하고 답변 드립니다.(이전질문 확인사항)
WITH tech AS ( SELECT 'AAA' cd, '001' itsqf, TO_DATE('20130101', 'yyyymmdd') s_day, TO_DATE('20131231', 'yyyymmdd') e_day FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20130601', 'yyyymmdd'), TO_DATE('20160831', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20140101', 'yyyymmdd'), TO_DATE('20141231', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20140401', 'yyyymmdd'), TO_DATE('20140531', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20140801', 'yyyymmdd'), TO_DATE('20141130', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20150101', 'yyyymmdd'), TO_DATE('20151231', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20160101', 'yyyymmdd'), TO_DATE('20160331', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20200302', 'yyyymmdd'), TO_DATE('20200622', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '001', TO_DATE('20021223', 'yyyymmdd'), TO_DATE('20070930', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '006', TO_DATE('20060615', 'yyyymmdd'), TO_DATE('20060830', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '003', TO_DATE('20140613', 'yyyymmdd'), TO_DATE('20141130', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '004', TO_DATE('20161101', 'yyyymmdd'), TO_DATE('20161201', 'yyyymmdd') FROM dual UNION ALL SELECT 'AAA', '005', TO_DATE('20161201', 'yyyymmdd'), TO_DATE('20190702', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '001', TO_DATE('20100201', 'yyyymmdd'), TO_DATE('20110131', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '001', TO_DATE('20100201', 'yyyymmdd'), TO_DATE('20110708', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '001', TO_DATE('20101220', 'yyyymmdd'), TO_DATE('20110901', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '001', TO_DATE('20100301', 'yyyymmdd'), TO_DATE('20110228', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '001', TO_DATE('20110101', 'yyyymmdd'), TO_DATE('20110901', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '002', TO_DATE('20080406', 'yyyymmdd'), TO_DATE('20080721', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '002', TO_DATE('20080912', 'yyyymmdd'), TO_DATE('20081111', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '002', TO_DATE('20081101', 'yyyymmdd'), TO_DATE('20090721', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '002', TO_DATE('20081117', 'yyyymmdd'), TO_DATE('20081230', 'yyyymmdd') FROM dual UNION ALL SELECT 'BBB', '002', TO_DATE('20081117', 'yyyymmdd'), TO_DATE('20090721', 'yyyymmdd') FROM dual UNION ALL SELECT 'CCC', '005', TO_DATE('20140401', 'yyyymmdd'), TO_DATE('20140531', 'yyyymmdd') FROM dual UNION ALL SELECT 'CCC', '005', TO_DATE('20140801', 'yyyymmdd'), TO_DATE('20141130', 'yyyymmdd') FROM dual UNION ALL SELECT 'CCC', '005', TO_DATE('20150101', 'yyyymmdd'), TO_DATE('20151231', 'yyyymmdd') FROM dual UNION ALL SELECT 'CCC', '005', TO_DATE('20160101', 'yyyymmdd'), TO_DATE('20160331', 'yyyymmdd') FROM dual UNION ALL SELECT 'CCC', '005', TO_DATE('20200302', 'yyyymmdd'), TO_DATE('20200622', 'yyyymmdd') FROM dual UNION ALL SELECT 'DDD', '002', TO_DATE('19990104', 'yyyymmdd'), TO_DATE('19991130', 'yyyymmdd') FROM dual UNION ALL SELECT 'DDD', '005', TO_DATE('20070702', 'yyyymmdd'), TO_DATE('20080608', 'yyyymmdd') FROM dual UNION ALL SELECT 'DDD', '005', TO_DATE('20081208', 'yyyymmdd'), TO_DATE('20090630', 'yyyymmdd') FROM dual UNION ALL SELECT 'DDD', '005', TO_DATE('20130701', 'yyyymmdd'), TO_DATE('20140630', 'yyyymmdd') FROM dual ) SELECT cd , SUM(DECODE(itsqf, '001', days)) aa , SUM(DECODE(itsqf, '002', days)) ab , SUM(DECODE(itsqf, '003', days)) ac , SUM(DECODE(itsqf, '004', days)) ad , SUM(DECODE(itsqf, '005', days)) ae , SUM(DECODE(itsqf, '006', days)) af FROM (SELECT cd, itsqf , SUM(e - s + 1) days FROM (SELECT cd, itsqf , MIN(s_day) s , MAX(e_day) e FROM (SELECT cd, itsqf, s_day, e_day , SUM(flag) OVER(PARTITION BY cd, itsqf ORDER BY s_day, e_day) grp FROM (SELECT cd, itsqf, s_day, e_day , CASE WHEN s_day <= MAX(e_day) OVER(PARTITION BY cd, itsqf ORDER BY s_day, e_day ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) THEN 0 ELSE 1 END flag FROM tech ) ) GROUP BY cd, itsqf, grp ) GROUP BY cd, itsqf -- HAVING SUM(e - s + 1) BETWEEN 365*0 AND 365*1-1 HAVING SUM(e - s + 1) BETWEEN 365*1 AND 365*2-1 ) GROUP BY cd ORDER BY cd ;