A사 | B사 | C사 | |||||
품목 | 수량 | 단가 | 금액 | 단가 | 금액 | 단가 | 금액 |
AAA | 3 | 100 | 300 | 0 | 0 | 150 | 450 |
BBB | 2 | 0 | 0 | 200 | 400 | 0 | 0 |
CCC | 1 | 100 | 100 | 150 | 150 | 0 | 0 |
총합계 | 400 | 550 | 450 |
A사 | B사 | C사 | |||||
품목 | 수량 | 단가 | 금액 | 단가 | 금액 | 단가 | 금액 |
AAA | 3 | 100 | 300 | 0 | 0(450) | 150 | 450 |
BBB | 2 | 0 | 0(400) | 200 | 400 | 0 | 0(400) |
CCC | 1 | 100 | 100 | 150 | 150 | 0 | 0(150) |
총합계 | 800 | 1000 | 600 |
WITH T AS(
SELECT 'AAA' PRO, 3 CON , 100 A, 0 B, 150 C FROM DUAL UNION ALL
SELECT 'BBB' PRO, 2 CON , 0 A, 200 B, 0 C FROM DUAL UNION ALL
SELECT 'AAA' PRO, 1 CON , 100 A, 150 B, 0 C FROM DUAL )
SELECT *
FROM (
SELECT PRO,CON
,DECODE(A,0,GREATEST(A,B,C),A)
,DECODE(B,0,GREATEST(A,B,C),B)
,DECODE(C,0,GREATEST(A,B,C),C)
FROM T)
-- GREATEST 함수를 써서 단가를 바꾸었습니다 여기서 필요하신거 추가하시면 댈거 같아요
WITH T AS(
SELECT 'AAA' PRO, 3 CON , 100 A, 0 B, 150 C FROM DUAL UNION ALL
SELECT 'BBB' PRO, 2 CON , 0 A, 200 B, 0 C FROM DUAL UNION ALL
SELECT 'CCC' PRO, 1 CON , 100 A, 150 B, 0 C FROM DUAL )
SELECT PRO
,DECODE(GROUPING(PRO),1,'총합계',MIN(CON)) AS "수량"
,DECODE(GROUPING(PRO),1,' ',MIN(A)) AS "A사 단가"
, SUM(A*CON) AS "A사 금액"
,DECODE(GROUPING(PRO),1,' ',MIN(B)) AS "B사 단가"
, SUM(B*CON) AS "B사 금액"
,DECODE(GROUPING(PRO),1,' ',MIN(C)) AS "C사 단가"
, SUM(C*CON) AS "C사 금액"
FROM (
SELECT PRO,CON
,DECODE(A,0,GREATEST(A,B,C),A) A
,DECODE(B,0,GREATEST(A,B,C),B) B
,DECODE(C,0,GREATEST(A,B,C),C) C
FROM T)
GROUP BY ROLLUP (PRO)