0

所以我试图一次做几件事,我不知道如何把它们放在一起。

我的第一步是获取多个单元格并根据 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)`

这两个查询单独工作,但我如何将它们组合成一个查询?

4

2 回答 2

0

我假设您可以简单地用您的语句替换原始查询对该LE.Name字段的调用CASE,如下所示:

USE MAIN;
SELECT 
    ACT.ID AS Activity,
    STUFF 
      (
        (       
            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 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

这对你有用吗?

于 2014-06-04T17:41:41.807 回答
0

试一试:

Select ACT.ID AS Activity,
ISNULL(STUFF((Select distinct ',' + 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  
               FROM ActivityDemographicEthnicity ADE
               LEFT OUTER JOIN LookupEthnicity LE ON LE.ID  = ADE.ethnicityId 
               WHERE ACT.ID = ADE.ActivityID
               FOR XML PATH ('')),1,1,''), 'General Market') AS Ethnicity
FROM Activity ACT

您需要将整个STUFF函数包装ISNULL在一个 Activity 中以捕获 Activity 没有相应的 ActivityDemographicEthnicity 的情况。并select distinct在里面STUFF删除重复项。

SQL小提琴

于 2014-06-05T00:51:07.490 回答