2017-08-16 65 views
0

我有兩個表 文件, 文件類型讀取多行的列值不同分爲單列

文件將持有這兩個文件類型的內部和外部的所有客戶端 我想要提取的數據客戶端文件放在一行內部文件路徑和extenral文件路徑中。

下面是元數據

create table filetypes (
    filetypeid int primary key, 
    typename varchar(20) 
); 

create table files (
    fileid int primary key, 
    filetypeid int, 
    fiilepath nvarchar(200), 
    client_id int 
); 

insert into files values (10, 1,'\testpath9\\InternalFoldername', 1); 
insert into files values (11, 2,'\testpath2\\ExternalFoldername', 1); 
insert into files values (12, 1,'\testpath5\\InternalFoldername', 2); 
insert into files values (13, 2,'\testpath6\\ExternalFoldername', 3); 


insert into filetypes values (1,'Internal'); 
insert into filetypes values (2,'External'); 

我想有SQL去取像下面

client_id InternalPath     ExternalPath 
----------- ------------------------------ ------------------------------ 
1   \testpath9\\InternalFoldername \testpath2\\ExternalFoldername 
+0

如果你有3個文件類型?你想有3列嗎?嘗試使用數據透視,它可能會有幫助 – FLICKER

回答

0

試試這個SQL語句的結果,

select t1.internal_client as client_id, t1.InternalPath as InternalPath, 
t2.ExternalPath as ExternalPath from 
(select client_id as internal_client,fiilepath as InternalPath from files where filetypeid=1) t1 
left join 
(select client_id as external_client, fiilepath as ExternalPath from files where filetypeid=2)t2 
on t1.internal_client = t2.external_client 
where t1.InternalPath IS NOT NULL and t2.ExternalPath IS NOT NULL