쿼리 도움좀 부탁드립니다. 0 4 631

by 크리스트 [2016.08.29 13:22:03]


안녕하세요

몇일전에 질문을 했었는데 혼자 계속 해보다가 아직 해결하지 못해 도움을 얻고자 질문을 드립니다.

DB는 SYBASE입니다.

trdhis라는 테이블에 아래의 값이 들어가 있는데

제가 하고싶은 것은 group_gb의 번호별로 weight의 합이 50 이하이면 해당 번호의 각 첫 번째 행에만 50,000원을 더하고

50~100이면 70,000원을 더하고 다른행은 weight * cost를 하고싶습니다.

해당 테이블의 키는 cstcst, trn_date, trn_seq, product, btest, color 입니다.

cstcd trn_date trn_seq iobit product btest color weight cost amount group_gb
1051 20160801 1 2 테스트2 123 테스트2 22 2000 44000 1
1051 20160801 1 2 테스트중 123-3 테스트 11 2500 27500 1
1051 20160818 1 2 테스트 12월 12일 파랑 22 3000 66000 2
1051 20160804 1 2 테스트중 11 테스트임 22.4 3000 67200 2
1051 20160801 1 2 테스트중3 123 테스트3 88 2000 176000 3
                     
cstcd trn_date trn_seq iobit product btest color weight cost amount group_gb
1051 20160801 1 2 테스트2 123 테스트2 22 2000 94000 1
1051 20160801 1 2 테스트중 123-3 테스트 11 2500 27500 1
1051 20160818 1 2 테스트 12월 12일 파랑 22 3000 116000 2
1051 20160804 1 2 테스트중 11 테스트임 22.4 3000 67200 2
1051 20160801 1 2 테스트중3 123 테스트3 88 2000 246000 3

 

 

by 마농 [2016.08.29 14:15:07]
SELECT cstcd, trn_date, trn_seq, iobit, product, btest, color
     , weight, cost
     , weight * cost
     + CASE WHEN rn = 1 AND weight_tot <=  50 THEN 50000
            WHEN rn = 1 AND weight_tot <= 100 THEN 70000
            ELSE 0 END
       AS amount
     , group_gb
  FROM (SELECT m.cstcd, m.trn_date, m.trn_seq, m.product, m.btest, m.color
             , m.group_gb, m.iobit, m.weight, m.cost
             , SUM(s.weight) weight_tot
             , COUNT(CASE WHEN (s.cstcd < m.cstcd)
                            OR (s.cstcd = m.cstcd AND s.trn_date < m.trn_date)
                            OR (s.cstcd = m.cstcd AND s.trn_date = m.trn_date AND s.trn_seq < m.trn_seq)
                            OR (s.cstcd = m.cstcd AND s.trn_date = m.trn_date AND s.trn_seq = m.trn_seq AND s.product < m.product)
                            OR (s.cstcd = m.cstcd AND s.trn_date = m.trn_date AND s.trn_seq = m.trn_seq AND s.product = m.product AND s.btest < m.btest)
                            OR (s.cstcd = m.cstcd AND s.trn_date = m.trn_date AND s.trn_seq = m.trn_seq AND s.product = m.product AND s.btest = m.btest AND s.color < m.color)
                          THEN 1 END
                     ) + 1 rn
          FROM trdhis m
         INNER JOIN trdhis s
            ON m.group_gb = s.group_gb
         GROUP BY m.cstcd, m.trn_date, m.trn_seq, m.product, m.btest, m.color
             , m.group_gb, m.iobit, m.weight, m.cost
        ) a
 ORDER BY group_gb, rn
;

 


by 크리스트 [2016.08.29 16:09:00]

마농님 댓글 감사합니다

알려주신 쿼리로 from절에 where조건을 추가하여 했을 경우에

from 절의 Select의 weight_tot값이 틀리게 나옵니다.

where 조건에 where m.cstcd ='1051' and m.trn_date like '201608%'

이것만 추가했습니다.

cstcd trn_date trn_seq product btest color group_gb iobit weight cost weight_tot rn
1051 20160804 1 테스트중 11 테스트임 2 2 22.4 3000 1342.7 20
1051 20160818 1 테스트 12-12. 파랑 2 2 22 3000 1342.7 21
1051 20160801 1 테스트중2 123 테스트2 1 2 22 2000 2889.6 35
1051 20160801 1 테스트중 123-3 테스트 1 2 11 2500 2889.6 34
1051 20160801 1 테스트중3 123 테스트3 3 2 88 2000 683 10

 

 

 


by 마농 [2016.08.29 16:36:10]

m 과 s 모두 조건을 주셔야죠.

SELECT cstcd, trn_date, trn_seq, iobit, product, btest, color
     , weight, cost
     , weight * cost
     + CASE WHEN rn = 1 AND weight_tot <=  50 THEN 50000
            WHEN rn = 1 AND weight_tot <= 100 THEN 70000
            ELSE 0 END
       AS amount
     , group_gb
  FROM (SELECT m.cstcd, m.trn_date, m.trn_seq, m.product, m.btest, m.color
             , m.group_gb, m.iobit, m.weight, m.cost
             , SUM(s.weight) weight_tot
             , COUNT(CASE WHEN (s.trn_date < m.trn_date)
                            OR (s.trn_date = m.trn_date AND s.trn_seq < m.trn_seq)
                            OR (s.trn_date = m.trn_date AND s.trn_seq = m.trn_seq AND s.product < m.product)
                            OR (s.trn_date = m.trn_date AND s.trn_seq = m.trn_seq AND s.product = m.product AND s.btest < m.btest)
                            OR (s.trn_date = m.trn_date AND s.trn_seq = m.trn_seq AND s.product = m.product AND s.btest = m.btest AND s.color < m.color)
                          THEN 1 END
                     ) + 1 rn
          FROM trdhis m
         INNER JOIN trdhis s
            ON m.group_gb = s.group_gb
           AND m.cstcd    = s.cstcd
         WHERE m.cstcd    = '1051'
           AND m.trn_date LIKE '201608%'
           AND s.trn_date LIKE '201608%'
         GROUP BY m.cstcd, m.trn_date, m.trn_seq, m.product, m.btest, m.color
             , m.group_gb, m.iobit, m.weight, m.cost
        ) a
 ORDER BY group_gb, rn
;

 


by 크리스트 [2016.08.29 22:25:57]

m과 s  둘다 조건을 줘야하는군요..

마농님 정말 감사합니다.

쿼리문 보고 공부좀 해야겠네요 ㅎㅎ

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