所以我试图一次做几件事,我不知道如何把它们放在一起。
我的第一步是获取多个单元格并根据 id 将它们组合成一行。
USE MAIN;
Select ACT.ID AS Activity
,STUFF (( Select ',' + LE.Name AS [text()]
FROM ActivityDemographicEthnicity ADE
LEFT OUTER JOIN LookupEthnicity LE
ON LE.ID = ADE.ethinicityId
WHERE ACT.ID = ADE.ActivityID
FOR XML PATH ('')),1,1,'')
AS Ethnicity
FROM Activity ACT
这很好用并产生如下内容:
ID Ethnicity
------------------------------------------
123 African, African American, Caucasian
125 Caucasian
128 NULL
但我想要这样的东西:
ID Ethnicity
----------------------------------------
123 African American, Caucasian
125 Caucasian
128 General Market
我通过 case 语句定义了新值:
USE Main;
(Select CASE
When LE.Name is null THEN 'General Market'
When LE.Name = 'African' THEN 'African American'
When LE.Name = 'American Indian/Native American' Then 'American Indian'
WHEN LE.NAME = 'Cambodian' Then 'Asian'
When LE.Name = 'Chinese' Then 'Asian'
When LE.Name = 'Filipino' Then 'Asian'
When LE.Name = 'Hmong' Then 'Asian'
WHEN LE.NAME = 'Japanese' THEN 'Asian'
WHEN LE.Name = 'Korean' Then 'Asian'
WHEN LE.Name = 'Laotian' Then 'Asian'
WHEN LE.Name = 'Vietnamese' Then 'Asian'
WHEN LE.Name = 'Armenian' Then 'Other'
WHEN LE.Name = 'Russian' Then 'Other'
WHEN LE.Name = 'Ukranian' Then 'Other'
WHEN LE.Name = 'Other (specify)' Then 'Other'
Else LE.Name
End 'Ethnicity'
From ActivityDemographicEthnicity ADE
LEFT JOIN LookupEthnicity LE
ON LE.ID = ADE.ID)`
这两个查询单独工作,但我如何将它们组合成一个查询?