日期:2014-05-17  浏览次数:20674 次

sql server关于合并列的问题
select distinct dbo.PatientInfo.ID AS 序号, dbo.PatientInfo.Name AS 姓名, dbo.PatientInfo.Age AS 年龄, dbo.PatientInfo.CaseNO AS 病历号, dbo.PatientInfo.RegistrationNO AS 登记号,dbo.PatientInfo.SerialNO AS 编号, dbo.PatientInfo.IC AS 身份证号 ,(select SUM(DateNO)  from Hospitalization where Hospitalization.PatientInfoID=PatientInfo.ID)  住院天数 ,STUFF((Select ','+'宫腔粘连' from GongQiangZhanLian where PatientInfo.ID = GongQiangZhanLian.PatientInfoID  for xml path ('')),1,1,'')   as 病种 

FROM         dbo.PatientInfo
left join GongQiangZhanLian on PatientInfo.ID = GongQiangZhanLian.PatientInfoID  
GO

这是一条查询语句,,同样我现在要在病种列中再增加一条查询的“宫内病变”,用+的方式会只有同样有宫腔粘连和宫内病变的数据才会显示有病种,没有同时有数据的就显示null,求帮助

------解决方案--------------------
引用:
select distinct dbo.PatientInfo.ID AS 序号, dbo.PatientInfo.Name AS 姓名, dbo.PatientInfo.Age AS 年龄, dbo.PatientInfo.CaseNO AS 病历号, dbo.PatientInfo.RegistrationNO AS 登记号,dbo.PatientInfo.SerialNO AS 编号, dbo.PatientInfo.IC AS 身份证号 ,(select SUM(DateNO)  from Hospitalization where Hospitalization.PatientInfoID=PatientInfo.ID)  住院天数 ,
STUFF((Select isnull(','+'宫腔粘连','') from GongQiangZhanLian where PatientInfo.ID = GongQiangZhanLian.PatientInfoID  for xml path ('')),1,1,'') + STUFF((Select isnull(','+'宫内病变','') from GongQiangNeiBingBian where PatientInfo.ID = GongQiangNeiBingBian.PatientInfo_ID  for xml path ('')),1,1,'')   as 病种 

FROM         dbo.PatientInfo
left join GongQiangZhanLian on PatientInfo.ID = GongQiangZhanLian.PatientInfoID  
GO


上面的数据太多了  我就不引用了,这就是加了stuff之后的查询



试试这个:
select distinct dbo.PatientInfo.ID AS 序号, dbo.PatientInfo.Name AS 姓名, dbo.PatientInfo.Age AS 年龄, dbo.PatientInfo.CaseNO AS 病历号, dbo.PatientInfo.RegistrationNO AS 登记号,dbo.PatientInfo.SerialNO AS 编号, dbo.PatientInfo.IC AS 身份证号 ,(select SUM(DateNO)  from Hospitalization where Hospitalization.PatientInfoID=PatientInfo.ID)  住院天数 ,
isnull(STUFF((Select ','+'宫腔粘连' from GongQiangZhanLian where PatientInfo.ID = GongQiangZhanLian.PatientInfoID  for