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) 住院天数 ,
isnull(STUFF((Select ','+'宫腔粘连' from GongQiangZhanLian where PatientInfo.ID = GongQiangZhanLian.PatientInfoID for xml path ('')),1,1,''),'') +
isnull(STUFF((Select ','+'宫内病变' 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