PIVOT Oracle - Transform Multiple Row Data To Single Row With Multiple Columns, No Aggregate Data
I have a need to transfer the following data set: in which only the highlighted lines are the one of interest (Tag in ('LN','SN')) as I am only interested in SerialNumber and LotN
Solution 1:
You aren't doing anything with the description, which also varies with the tag. It isn't aggregated so it in the implicit 'group by', so you get separate rows in the result set.
You can either capture that too with another (dummy) aggregate:
select * from (
select * from TEST2 where tag in ('LN', 'SN')
)
PIVOT
(
max(value) as value, max(description) as description
for tag in ('LN' as ln, 'SN' as sn)
)
order by category, subcat, item, "Date";
Date SUBCAT CATEGOR IT LN_VALUE LN_DESCRIPTION SN_VALUE SN_DESCRIPTION
--------- ------ ------- -- ----------------- --------------- ----------------- ---------------
24-OCT-13 290223 1219576 25 1105618 Lot Number 3x12mm Serial Number
24-OCT-13 290223 1219576 28 1303757 Lot Number
18-JUN-15 354506 1219576 4 1403114 Lot Number
18-JUN-15 354506 1219576 9 7777777777 Lot Number 9.999999999999E12 Serial Number
Or more likely exclude it from the intermediate result set if you don't want it, by specify the columns you do want instead of using *:
select * from (
select category, subcat, item, "Date", tag, value
from TEST2 where tag in ('LN', 'SN')
)
PIVOT
(
max(value) for tag in ('LN' as ln, 'SN' as sn)
)
order by category, subcat, item, "Date";
CATEGOR SUBCAT IT Date LN SN
------- ------ -- --------- ----------------- -----------------
1219576 290223 25 24-OCT-13 1105618 3x12mm
1219576 290223 28 24-OCT-13 1303757
1219576 354506 4 18-JUN-15 1403114
1219576 354506 9 18-JUN-15 7777777777 9.999999999999E12
Solution 2:
Get table data in pivot xml
with a as (select to_char(xmltype.getclobval(JOB_XML)) k from (
select * from (select ename,job from emp)
pivot xml ( max(ename) for job in (select job from emp))))
SELECT EXTRACTVALUE(VALUE(xml_list), '//column[1]') AS interface_no
,EXTRACTVALUE(VALUE(xml_list), '//column[2]') AS interface_name_a
FROM TABLE(XMLSEQUENCE(EXTRACT(XMLType('<?xml version="1.0" encoding="UTF-8"?>'||(select * from a) ), 'PivotSet/item'))) xml_list;
Post a Comment for "PIVOT Oracle - Transform Multiple Row Data To Single Row With Multiple Columns, No Aggregate Data"