简体   繁体   English

我怎么能SQL枢轴这个选择?

[英]how can i sql pivot this select?

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

the above code result sin this: 上面的代码结果如下:

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

I want it to return one record with 10 fields: 我希望它返回一个包含10个字段的记录:

Cost, Green, Requirements, Yellow, Schedule, NULL, Technical, NULL, Testing, NULL 成本,绿色,需求,黄色,进度,NULL,技术,NULL,测试,NULL

Can i pivot on cid? 我可以在CID上旋转吗?

Instead of having seperate columns, could you live with one column? 不用单独的列,您可以住一列吗? Like this? 像这样?

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,'') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM