此查询为我提供了所需的所有信息,但是如果可能的话,我希望以不同的方式显示它。目前的结果:http://i.imgur.com/BFKGFSx.jpg
DECLARE @MainHospital varchar(50)='HOSPITAL 1';
SELECT MainEmail, chkOutpatient, chkPartB
FROM SurveyPicList
WHERE MainHospital = @MainHospital
GROUP BY MainHospital, MainEmail, chkOutpatient, chkPartB
我正在尝试返回2个不同的MainEmail comma-delimited列表,如果它们在chkOutpatient和chkPartB中为“on”。因此,结果只有2个数据单元。 chkOutpatient的1个标题,其中包含逗号稀释的电子邮件列表=“on”,以及chkPartB的1个标题。
那么对于chkPartB,这样的事情呢?http://i.imgur.com/RFlV24Q.jpg
SELECT DISTINCT ', ' + MainEmail AS chkPartB
FROM SurveyPicList
WHERE MainHospital = @MainHospital
AND chkPartB = 'on'
如果我的问题不清楚或者我是否需要提供更多信息,请告诉我。
答案 0 :(得分:1)
WITH Outpatients AS (
SELECT DISTINCT MainEmail
FROM SurveyPicList
WHERE MainHospital = @MainHospital
AND chkOutpatient = 'on'
)
,OutpatientsRawCsv AS (
SELECT (
SELECT ',' + MainEmail
FROM Outpatients
FOR XML PATH('')
) AS Csv
)
,PartBs AS (
SELECT DISTINCT MainEmail
FROM SurveyPicList
WHERE MainHospital = @MainHospital
AND chkPartB = 'on'
)
,PartBRawCsv AS (
SELECT (
SELECT ',' + MainEmail
FROM PartBs
FOR XML PATH('')
) AS Csv
)
SELECT STUFF(OutpatientsRawCsv.Csv, 1, 1, '') AS OutpatientsCsv
,STUFF(PartBRawCsv.Csv, 1, 1, '') AS PartBCsv
FROM OutpatientsRawCsv
CROSS JOIN PartBRawCsv