2011-02-11 62 views
1
SELECT sc.CID,sc.CodeName as OverviewText,scRAG.CodeName as RAGStatusText 
FROM StatusCode sc 
    LEFT OUTER JOIN ProjectOverview po ON sc.CID = po.ProjectOverviewCID AND po.ProjectId = 180 
    LEFT OUTER JOIN StatusCode scRAG ON po.RAGStatusCID = scRAG.CID 
WHERE sc.SCID = 18 

這上面的代碼的結果罪:我怎樣才能讓數據透視這個選擇?

CID OverviewText RAGStatusText 
153 Cost   Green 
154 Requirements Yellow 
155 Schedule NULL 
156 Technical NULL 
157 Testing   NULL 

我希望它有10個字段返回一條記錄:

成本,綠色,要求,黃色, 計劃,NULL, 技術, NULL, Testing,NULL

我可以在cid上轉動嗎?

+1

這是[至少你的第6個關鍵問題](http://stackoverflow.com/search?q=user%3A352157+pivot)據我所知。你試過什麼了? – 2011-02-11 16:43:23

回答

0

而不是有獨立的列,你可以住一列嗎?喜歡這個?

create table #t1([CID] int 
,[OverviewText] varchar(12) 
,[RAGStatusText] varchar(6)) 

insert #t1 values (153,'Cost','Green') 
insert #t1 values (154,'Requirements','Yellow') 
insert #t1 values (155,'Schedule',NULL) 
insert #t1 values (156,'Technical',NULL) 
insert #t1 values (157,'Testing',NULL) 

select StringToSpit = stuff((select 
[text()]=','+[OverviewText]+','+isnull([RAGStatusText],'NULL') 
from #t1 
for xml path('')),1,1,'')