if object_id('tempdb.dbo.#ctp', 'u') is not null
drop table #ctp ;
create table #ctp(id int, mastername varchar(16)) ;
insert into #ctp values(1, 'Big Boy') ;
if object_id('tempdb.dbo.#client', 'u') is not null
drop table #client ;
create table #client(id int, name varchar(16), type int) ;
insert into #client values(1, 'ABC', 5) ;
insert into #client values(2, 'XYZ', 6) ;
if object_id('tempdb.dbo.#ctpclient', 'u') is not null
drop table #ctpclient ;
create table #ctpclient(id int, ctpfk int, clientfk int) ;
insert into #ctpclient values(1, 1, 1) ;
insert into #ctpclient values(2, 1, 2) ;
select tp.mastername
, c.name
, c.type
, cc.ctpfk
, cc.clientfk
from #ctp tp
join #ctpclient cc
on tp.id = cc.ctpfk
join #client c
on c.id = cc.clientfk
;
電流輸出SQL Server - 從兩行,一列到一行,兩列?
mastername|name|type
Big Boy|ABC|5
Big Boy|XYZ|6
而不是輸出的兩排,我想輸出到如下:
mastername|nameone|nametwo
Big Boy | ABC | XYZ
什麼是做這個因爲我的最佳方式一個多對多的表,如#ctpclient?
閱讀關於[PIVOT](https://technet.microsoft.com/en-us/library/ms177410(v = sql.105).aspx) –