PIVOT Oracle - 将多行数据转换为具有多列的单行,无聚合数据

PIVOT Oracle - transform multiple row data to single row with multiple columns, no aggregate data

我需要传输以下数据集:

其中只有突出显示的行是感兴趣的(在 ('LN','SN') 中标记),因为我只对产品的序列号和批号感兴趣。 我想将上面的数据集转换成下面的数据集:

它在 1 行中列出了产品及其序列号和批号。

在网上看了一些资料后,我觉得 PIVOT 可能是我需要的。但是,我正在为声明的技术方面而苦苦挣扎。

我试过:

select * from (
select * from TEST2 where tag in ('LN','SN')
)
PIVOT
(
  max(value)
  for tag in ('LN','SN')
)
order by category,subcat,item,"Date"

但这不会生成我想要的输出。有什么建议吗? PIVOT 是要使用的正确语句还是在这种情况下是否有其他更合适的语句?我意识到 PIVOT 需要一个聚合函数,但我不计算或添加任何东西。请指教

下面是我的测试table及其数据

  CREATE TABLE "TEST2" 
   (    "Date" DATE, 
    "SUBCAT" VARCHAR2(6 BYTE), 
    "CATEGORY" VARCHAR2(7 BYTE), 
    "VALUE" VARCHAR2(17 BYTE), 
    "ITEM" VARCHAR2(2 BYTE), 
    "DESCRIPTION" VARCHAR2(15 BYTE), 
    "TAG" VARCHAR2(3 BYTE)
   ) 


Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'25','OTHER ATTRIBUTE','OA');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'25','SOME COMMENTS','SC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','1105618','25','Lot Number','LN');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','12','25','NOT RELEVANT','NR');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','45','25','NOT USE','NU');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','-1','25','DO NOT CARE','DC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','3x12mm','25','Serial Number','SN');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'25','ABC','ABC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'25','Whatever','DEF');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'28','SOME ATTRIBUTE','SA');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'28','OTHER ATTRIBUTE','OA');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','isq: 75, 80','28','Other Comments','OC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'28','SOME COMMENTS','SC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','1303757','28','Lot Number','LN');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','12','28','NOT RELEVANT','NR');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','40','28','NOT USE','NU');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576','0','28','DO NOT CARE','DC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'28','ABC','ABC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),'290223','1219576',null,'28','Whatever','DEF');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576',null,'4','SOME ATTRIBUTE','SA');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576',null,'4','OTHER ATTRIBUTE','OA');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576','1403114','4','Lot Number','LN');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576','11','4','NOT RELEVANT','NR');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576','50','4','NOT USE','NU');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576','0','4','DO NOT CARE','DC');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576',null,'4','Whatever','DEF');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576','7777777777','9','Lot Number','LN');
Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR'),'354506','1219576','9.999999999999E12','9','Serial Number','SN');

您没有对描述进行任何操作,描述也因标签而异。它没有聚合,所以它在隐式 'group by' 中,所以你在结果集中得到单独的行。

您也可以使用另一个(虚拟)聚合捕获它:

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  

或者如果您不想要它,更有可能将它从中间结果集中排除,方法是指定您确实想要的列而不是使用 *:

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

在数据透视xml

中获取table数据

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;