如何在没有聚合的情况下将此行转换为Oracle中的列?

时间:2017-10-11 16:31:31

标签: sql oracle plsql

大家好,我有下表,它是由一个可怕的开发者设计的

该表包含USERS指纹及其USERID的数据。 来自(1,2,3,4,5,6,7,8,9,10,11,12,13,14)的FINGERID ISLOCKED并且ISDUPLICATE值为10 ...每个FINGERID都有图像存储在BLOB

Name        Null     Type          
----------- -------- ------------- 
ROW_ID               NUMBER        
USERID      NOT NULL NVARCHAR2(75) 
TEMPLATE             BLOB          
FINGERID    NOT NULL NVARCHAR2(20) 
ISLOCKED             NUMBER(3)     
ISDUPLICATE          NUMBER(3)

我希望以横向方式显示它,但是从2周开始没有运气......下面是我想要的选择语句结构

Name                    Null     Type          
----------------------- -------- -------------        
USERID                  NOT NULL NVARCHAR2(75) 
FINGERID_1                       NVARCHAR2(20) 
FINGERID_1_TEMPLATE              BLOB          
FINGERID_1_ISLOCKED              NUMBER(3)     
FINGERID_1_ISDUPLICATE           NUMBER(3)     
FINGERID_2                       NVARCHAR2(20) 
FINGERID_2_TEMPLATE              BLOB          
FINGERID_2_ISLOCKED              NUMBER(3)     
FINGERID_2_ISDUPLICATE           NUMBER(3)     
FINGERID_3                       NVARCHAR2(20) 
FINGERID_3_TEMPLATE              BLOB          
FINGERID_3_ISLOCKED              NUMBER(3)     
FINGERID_3_ISDUPLICATE           NUMBER(3)     
FINGERID_4                       NVARCHAR2(20) 
FINGERID_4_TEMPLATE              BLOB          
FINGERID_4_ISLOCKED              NUMBER(3)     
FINGERID_4_ISDUPLICATE           NUMBER(3)     
FINGERID_5                       NVARCHAR2(20) 
FINGERID_5_TEMPLATE              BLOB          
FINGERID_5_ISLOCKED              NUMBER(3)     
FINGERID_5_ISDUPLICATE           NUMBER(3)     
FINGERID_6                       NVARCHAR2(20) 
FINGERID_6_TEMPLATE              BLOB          
FINGERID_6_ISLOCKED              NUMBER(3)     
FINGERID_6_ISDUPLICATE           NUMBER(3)     
FINGERID_7                       NVARCHAR2(20) 
FINGERID_7_TEMPLATE              BLOB          
FINGERID_7_ISLOCKED              NUMBER(3)     
FINGERID_7_ISDUPLICATE           NUMBER(3)     
FINGERID_8                       NVARCHAR2(20) 
FINGERID_8_TEMPLATE              BLOB          
FINGERID_8_ISLOCKED              NUMBER(3)     
FINGERID_8_ISDUPLICATE           NUMBER(3)     
FINGERID_9                       NVARCHAR2(20) 
FINGERID_9_TEMPLATE              BLOB          
FINGERID_9_ISLOCKED              NUMBER(3)     
FINGERID_9_ISDUPLICATE           NUMBER(3)     
FINGERID_10                      NVARCHAR2(20) 
FINGERID_10_TEMPLATE             BLOB          
FINGERID_10_ISLOCKED             NUMBER(3)     
FINGERID_10_ISDUPLICATE          NUMBER(3)
FINGERID_11                      NVARCHAR2(20) 
FINGERID_11_TEMPLATE             BLOB          
FINGERID_11_ISLOCKED             NUMBER(3)     
FINGERID_11_ISDUPLICATE          NUMBER(3)

我尝试过使用此代码但没有运气,因为我无法显示TEMPLATEBLOB),ISLOCKEDISDUPLICATED 对于每个FINGERID,因为它不能与group by一起使用,pivot不是一个好选择,因为它需要聚合...我只是想显示数据是如何但是以水平方式为每个USERID。

`select 
       USERID,
       SUM(case when FINGERID=1 then 1 END) as FINGERID_1,  
       SUM(case when FINGERID=2 then 2 END) as FINGERID_2,
       SUM(case when FINGERID=3 then 3 END) as FINGERID_3,
       SUM(case when FINGERID=4 then 4 END) as FINGERID_4,
       SUM(case when FINGERID=5 then 5 END) as FINGERID_5,
       SUM(case when FINGERID=6 then 6 END) as FINGERID_6,
       SUM(case when FINGERID=7 then 7 END) as FINGERID_7,
       SUM(case when FINGERID=8 then 8 END) as FINGERID_8,
       SUM(case when FINGERID=9 then 9 END) as FINGERID_9,
       SUM(case when FINGERID=10 then 10 END) as FINGERID_10,
       SUM(case when FINGERID=11 then 11 END) as FINGERID_11,
       SUM(case when FINGERID=12 then 12 END) as FINGERID_12,
       SUM(case when FINGERID=13 then 13 END) as FINGERID_13,
       SUM(case when FINGERID=14 then 14 END) as FINGERID_14
       FROM TBLUSERFINGER group by userid;` 

2 个答案:

答案 0 :(得分:2)

这些数据类型确实存在问题。相反,您可以多次使用LEFT JOIN

select u.userid,
       f1.fingerid as fingerid_1,
       f1.template as fingerid_1_template,
       f1.islocked as fingerid_1_islocked,
       f1.isduplicate as fingerid_1_isduplicate,
       . . .
from (select distinct userid from TBLUSERFINGER) u left join
     TBLUSERFINGER f1
     on f1.userid = u.userid and f1.fingerid = 1 left join
     TBLUSERFINGER f2
     on f2.userid = u.userid and f2.fingerid = 2 left join
     . . .
     TBLUSERFINGER f14
     on f14.userid = u.userid and f14.fingerid = 14 ;

答案 1 :(得分:0)

这是另一种方法。如果你的表非常大并且多次加入自身太慢,你可以将除了blob之外的所有东西都移出来,并将它存储在可以找到blob的地方。例如:

create table testx (
ROW_ID               NUMBER,        
USERID       NVARCHAR2(75) NOT NULL,
TEMPLATE             BLOB     ,     
FINGERID     NVARCHAR2(20) NOT NULL,
ISLOCKED             NUMBER(3)     ,
ISDUPLICATE          NUMBER(3)
);

create unique index testx_uidx on testx(userid, fingerid);

-- insert some test data
insert into testx values (1,'UID1',RAWTOHEX(UTL_RAW.CAST_TO_RAW('LOB1')),'1',1,0);
insert into testx values (2,'UID1',RAWTOHEX(UTL_RAW.CAST_TO_RAW('LOB2')),'2',1,0);
insert into testx values (3,'UID1',RAWTOHEX(UTL_RAW.CAST_TO_RAW('LOB3')),'3',0,0);
insert into testx values (4,'UID2',RAWTOHEX(UTL_RAW.CAST_TO_RAW('LOB1')),'1',1,0);
insert into testx values (5,'UID2',RAWTOHEX(UTL_RAW.CAST_TO_RAW('LOB2')),'2',1,0);
insert into testx values (6,'UID2',RAWTOHEX(UTL_RAW.CAST_TO_RAW('LOB3')),'3',0,0);
commit;

-- pivot out what we can, show rowid to blob data
select * from (
  select userid, rowid as template_rowid, fingerid,islocked,isduplicate
  from testx
)
pivot ( 
  max(template_rowid) rowid_of_template, max(islocked) mislocked, max(isduplicate) misduplicate
  for fingerid in ('1' fid1,'2' fid2,'3' fid3,'4' fid4,'5' fid5,'6' fid6,'7' fid7,'8' fid8,'9' fid9,'10' fid10,'11' fid11,'12' fid12,'13' fid13,'14' fid14)
);

输出:

USERID,FID1_ROWID_OF_TEMPLATE,FID1_MISLOCKED,FID1_MISDUPLICATE,FID2_ROWID_OF_TEMPLATE,FID2_MISLOCKED,FID2_MISDUPLICATE,FID3_ROWID_OF_TEMPLATE,FID3_MISLOCKED,FID3_MISDUPLICATE,FID4_ROWID_OF_TEMPLATE,FID4_MISLOCKED,FID4_MISDUPLICATE,FID5_ROWID_OF_TEMPLATE,FID5_MISLOCKED,FID5_MISDUPLICATE,FID6_ROWID_OF_TEMPLATE,FID6_MISLOCKED,FID6_MISDUPLICATE,FID7_ROWID_OF_TEMPLATE,FID7_MISLOCKED,FID7_MISDUPLICATE,FID8_ROWID_OF_TEMPLATE,FID8_MISLOCKED,FID8_MISDUPLICATE,FID9_ROWID_OF_TEMPLATE,FID9_MISLOCKED,FID9_MISDUPLICATE,FID10_ROWID_OF_TEMPLATE,FID10_MISLOCKED,FID10_MISDUPLICATE,FID11_ROWID_OF_TEMPLATE,FID11_MISLOCKED,FID11_MISDUPLICATE,FID12_ROWID_OF_TEMPLATE,FID12_MISLOCKED,FID12_MISDUPLICATE,FID13_ROWID_OF_TEMPLATE,FID13_MISLOCKED,FID13_MISDUPLICATE,FID14_ROWID_OF_TEMPLATE,FID14_MISLOCKED,FID14_MISDUPLICATE
"UID1","AAzWrKACUAAA1ffAAA",1,0,"AAzWrKACUAAA1ffAAB",1,0,"AAzWrKACUAAA1ffAAC",0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
"UID2","AAzWrKACUAAA1ffAAD",1,0,"AAzWrKACUAAA1ffAAE",1,0,"AAzWrKACUAAA1ffAAF",0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

然后,应用程序可以根据需要使用rowid快速执行查找(例如显示blob图像)。