回答

收藏

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

技术问答 技术问答 223 人阅读 | 0 人回复 | 2023-09-12

只有突出显示的行是感兴趣的行( LN”,“SN)标记,因为我只对产品的序列号和批号感兴趣。我想将上述数据集转换为以下数据集# _% a  ^1 L; X$ Y6 s
在一列中列出产品及其序列号和批号的位置。
. b0 p6 s# l" N, B, E% N: b( z在网上阅读后,我想可能是PIVOT需要什么。然而,我在声明的技术上挣扎。) ^" s1 r7 V+ S" Y! Y; }* x, O
我试过了:
- h$ S! J( T1 {) f. D5 w4 L. y) p" aselect * 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需要聚合函数,但是我不计数也不添加任何东西。请指教。
9 Y/ Z6 H( p- T8 _7 n以下是我的测试表及其数据
5 E) d- J7 u1 ]; X1 P        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-RR29023null,'25','SOME COMMENTS','SC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023'1105618','25','Lot Number','LN');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR'),29023,1219576NOT RELEVANT','NR');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023,1219576,45,25NOT USE','NU');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023-一、二五、DO NOT CARE','DC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023x12mm,Serial Number','SN');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'25','ABC','ABC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'25','Whatever','DEF');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'28','SOME ATTRIBUTE','SA');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'28','OTHER ATTRIBUTE','OA');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023isq:28Other Comments','OC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'28','SOME COMMENTS','SC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023年,13037777Lot Number','LN');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023,1219576,12NOT RELEVANT','NR');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023,1219576,40,28NOT USE','NU');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023DO NOT CARE','DC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'28','ABC','ABC');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('24-OCT-13','DD-MON-RR29023null,'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-RR354506Lot Number','LN');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR,354506,1219NOT RELEVANT','NR');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR,354506,1219576,50,4NOT 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,77777777777,9Lot Number','LN');Insert into TEST2 ("Date",SUBCAT,CATEGORY,VALUE,ITEM,DESCRIPTION,TAG) values (to_date('18-JUN-15','DD-MON-RR,354506,1219576.999999999999E十二、九、Serial Number','SN);                 : d; S+ j( `2 @
    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则