그림에서 보는것과 같이 TOTA_AMNT와 TOTA_VATS를 나타내고 싶습니다 대부분 다해결을했는데 이걸모르겠네요 0 3 728

by darkload [SQL Query] [2018.01.04 10:04:51]


회사.jpg (62,292Bytes)

SELECT b.cust_name,c.item_name, A.chgo_date,A.cust_code,A.item_code,A.chgo_qnty,A.chgo_dann,A.chgo_amnt,(chgo_amnt*0.1) as chgo_vats
   FROM ITEM_CHGO as A with(nolock)
       LEFT OUTER JOIN
       CUST_INFO B
    ON a.cust_code = b.cust_Code
    left outer join
	   item_info c
	   on a.item_code = c.item_code
   where c.cust_code = '0009'

 

by 마농 [2018.01.04 10:17:32]

1. 첫번째 쿼리는 왜 Distinct 를 사용했을까요?
  - Distinct 가 적절한지 검토해 보세요.
2. 두번째 쿼리의 그룹바이 기준이 이상하네요.
  - 두번째 쿼리를 정확하게 완성해 주세요.
3. 쿼리를 합칠때는
  - 보통 조인연산(JOIN) 또는 집합연산(UNION ALL) 이 사용됩니다.
  - 원하시는 결과집합의 형태에 따라 어떻게 합쳐야 할지 정해집니다.
  - 막연하게 합치고 싶다고 해서 합쳐지는게 아닙니다.
  - 정확한 결과표의 형태를 그려주세요.


by darkload [2018.01.04 10:50:21]

수정했습니다.


by 마농 [2018.01.04 11:10:38]
-- 1. Union All
SELECT b.cust_name
     , c.item_name
     , a.chgo_date
     , a.cust_code
     , a.item_code
     , a.chgo_qnty
     , a.chgo_dann
     , a.chgo_amnt
     , a.chgo_amnt * 0.1 AS chgo_vats
  FROM item_chgo AS a WITH(NOLOCK)
  LEFT OUTER JOIN
       cust_info b
    ON a.cust_code = b.cust_Code
  LEFT OUTER JOIN
       item_info c
    ON a.item_code = c.item_code
 WHERE a.cust_code = '0009'
 UNION ALL
SELECT null AS cust_name
     , null AS item_name
     , null AS chgo_date
     , null AS cust_code
     , null AS item_code
     , null AS chgo_qnty
     , null AS chgo_dann
     , SUM(a.chgo_amnt)       AS chgo_amnt
     , SUM(a.chgo_amnt) * 0.1 AS chgo_vats
  FROM item_chgo AS a WITH(NOLOCK)
 WHERE a.cust_code = '0009'
;
-- 2. Grouping Sets
SELECT b.cust_name
     , c.item_name
     , a.chgo_date
     , a.cust_code
     , a.item_code
     , a.chgo_qnty
     , a.chgo_dann
     , SUM(a.chgo_amnt)       AS chgo_amnt
     , SUM(a.chgo_amnt) * 0.1 AS chgo_vats
  FROM item_chgo AS a WITH(NOLOCK)
  LEFT OUTER JOIN
       cust_info b
    ON a.cust_code = b.cust_Code
  LEFT OUTER JOIN
       item_info c
    ON a.item_code = c.item_code
 WHERE a.cust_code = '0009'
 GROUP BY GROUPING SETS ( ( b.cust_name
                          , c.item_name
                          , a.chgo_date
                          , a.cust_code
                          , a.item_code
                          , a.chgo_qnty
                          , a.chgo_dann
                          ) , () )
;

 

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