1번
select paykind, sum(payinwon)as pay1종류
from sha_pay2
where ID=1 and
paykind is not null
group by paykind
결과
카드 2000
현금 160000
cms 2000
2번
select paykind2, sum(payinwon2)as pay2종류
from sha_pay2
where ID=1
and paykind2 is not null
group by paykind2
결과
카드 90000
현금 20000
이두개 의 쿼리를 합쳐서
카드 2000 카드 90000
현금 160000 현금 20000
cms 2000
또는
이런식으로 만들 고 싶은데 고수분들 부탁드립니다 ㅜ
카드 2000 90000
현금 160000 20000
cms 2000 0
1 2 3 4 5 6 7 8 9 10 11 12 | WITH sha_pay2(id, paykind, payinwon, paykind2, payinwon2) AS ( SELECT 1, '카드' , 2000, Null , Null FROM dual UNION ALL SELECT 1, '현금' , 160000, Null , Null FROM dual UNION ALL SELECT 1, 'cms' , 2000, '카드' , 90000 FROM dual UNION ALL SELECT 1, Null , Null , '현금' , 20000 FROM dual ) SELECT * FROM ( SELECT * FROM sha_pay2 WHERE id = 1) UNPIVOT ((paykind, payinwon) FOR gb IN ((paykind, payinwon) AS 1, (paykind2, payinwon2) AS 2)) PIVOT ( SUM (payinwon) FOR gb IN (1 payinwon1, 2 payinwon2)) ; |