SELECT 'POTATO' NAME, '20190715' sal_date, '20190721' sold_out, 165000 QTY, 'Y' GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190728' sold_out, 35000 QTY, 'N'GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190728' sold_out, 118000 QTY,'Y' GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190731' sold_out, 130000 QTY,'Y' GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190728' sold_out, 52000 QTY, 'Y' GB FROM DUAL
아래 결과값으 얻고 싶습니다. havning, count을 이용하여 작성중인데 잘 안되네요 ㅠ
NAME | SAL_DATE | SOLD_OUT | QTY | GB |
POTATO | 20190715 | 20190721 | 165000 | Y |
POTATO | 20190715 | 20190728 | 35000 | N |
POTATO | 20190715 | 20190728 | 170000 | Y |
POTATO | 20190715 | 20190731 | 130000 | Y |
SELECT NAME , SAL_DATE , SOLD_OUT , SUM(QTY) AS QTY , GB FROM ( SELECT 'POTATO' NAME, '20190715' sal_date, '20190721' sold_out, 165000 QTY, 'Y' GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190728' sold_out, 35000 QTY, 'N'GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190728' sold_out, 118000 QTY,'Y' GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190731' sold_out, 130000 QTY,'Y' GB FROM DUAL UNION ALL SELECT 'POTATO' NAME, '20190715' sal_date, '20190728' sold_out, 52000 QTY, 'Y' GB FROM DUAL ) GROUP BY NAME , SAL_DATE , SOLD_OUT , GB ORDER BY SAL_DATE , SOLD_OUT
제 입맛대로지만 이렇게하면 나오지 않을까요??....
합치고자 하는 기준에 대한 설명이 전혀 없네요?
WITH t AS ( SELECT 'POTATO' name, '20190715' sal_date, '20190721' sold_out, 165000 qty, 'Y' gb FROM dual UNION ALL SELECT 'POTATO', '20190715', '20190728', 35000, 'N' FROM dual UNION ALL SELECT 'POTATO', '20190715', '20190728', 118000, 'Y' FROM dual UNION ALL SELECT 'POTATO', '20190715', '20190731', 130000, 'Y' FROM dual UNION ALL SELECT 'POTATO', '20190715', '20190728', 52000, 'Y' FROM dual ) SELECT name, sal_date, sold_out , SUM(qty) qty , gb FROM t GROUP BY name, sal_date, sold_out, gb ORDER BY name, sal_date, sold_out, gb ;