중복 합치기 0 2 920

by 캘린다 [Oracle 기초] [2019.06.10 16:14:21]



     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
by 아지 [2019.06.10 16:37:17]
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

 

 

제 입맛대로지만 이렇게하면 나오지 않을까요??....

 


by 마농 [2019.06.10 16:37:54]

합치고자 하는 기준에 대한 설명이 전혀 없네요?

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
;

 

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