Ok, take a look at below query and at the results:
SELECT *
FROM (SELECT
cs.cd, cs.dsc, cp.part_cd, cp.qty, cp.dllrs, cp.cu_type
FROM ck_startup cs
JOIN ck_price cp ON (cs.prd_type_cd = cp.prd_type_cd))
PIVOT (SUM(dllrs) AS dlllrs FOR (cu_type) IN ('A' AS a, 'AC' AS ac))
ORDER BY cd, qty
;
Output:
CD DSC PART_CD QTY A_DLLLRS AC_DLLLRS -------- ----------------- ---------- ------- ---------- ---------- 3D Stuff 1 100 10 3D Stuff 1 200 20 3D Stuff 1 300 30 30 DC Different stuff 1 100 50 50 DC Different stuff 1 200 100 100 DC Different stuff 1 300 150 DC Different stuff 1 400 200 DN2 Similar stuff 1 100 50 50 DN2 Similar stuff 1 200 100 100 DN2 Similar stuff 1 300 150 DN2 Similar stuff 1 400 200
It is not what you would expect, because I do not understand why you have different values in DLLRS_AC
column that are in the CK_PRICE
table? I mean, for example, why do you have 400
in last line of your output, not 200
? Why is this value doubled (as others are in DLLRS_AC
column)?
If you are using Oracle 10g, you can achieve the same result using DECODE
and GROUP BY
, take a look:
SELECT
cd,
dsc,
part_cd,
qty,
SUM(DECODE(cu_type, 'A', dllrs, NULL)) AS dllrs_a,
SUM(DECODE(cu_type, 'AC', dllrs, NULL)) AS dllrs_ac
FROM (
SELECT
cs.cd, cs.dsc, cp.part_cd, cp.qty, cp.dllrs, cp.cu_type
FROM ck_startup cs
JOIN ck_price cp ON (cs.prd_type_cd = cp.prd_type_cd)
)
GROUP BY cd, dsc, part_cd, qty
ORDER BY cd, qty;
Result is the same.
If you want to read more about pivoting, I recommend article by Tim Hall: Pivot and Unpivot at Oracle Base