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

其中只有突出显示的行是感兴趣的行((“ 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需要聚合函数,但是我不计数也不添加任何东西。请指教。

以下是我的测试表及其数据

  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

以上是 PIVOT Oracle-将多行数据转换为具有多列的单行,没有聚合数据 的全部内容, 来源链接: utcz.com/qa/401219.html

回到顶部