案例陈述有问题

时间:2011-07-06 22:40:46

标签: sql sql-server sql-server-2005 tsql sql-server-2008

我有以下查询:

SELECT  CASE WHEN [Question Order] IN ( 6, 11 )
             THEN CASE WHEN [Question Part Label] = 'Other (Please specify):'
                       THEN [Answer Text]
                       ELSE [Question Part Label]
                  END
             ELSE 'replace code here'
        END,[Respondent ID]
FROM    Results
WHERE  [Question Order] IN ( 6, 11 ) AND [Answer Label] = 'Yes'

现在我想替换这里代码'替换代码'

 select         
 stuff((select ','+T2.[Question Part Label]   from Results as T2 
   where T1.[Respondent ID] = T2.[Respondent ID]    for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '') 
   as Label from Results as T1 group by T1.[Respondent ID] 

当我这样做时,我收到以下错误

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

示例数据:

Respondent ID  [Question Order]  [Question Part Label]   [Answer Text]   [Answer Label]
124587          6                It was not clear          NULL           Yes
124587          6                Did not Undersstand       NULL           Yes
124589          6                Other (Please specify):   Not enough     Yes
125654          6                Too Fast                  NULL           Yes
124582          11               Not frequent              NULL           Yes

输出Shpuld是:

Respondent ID           [Question Part Label]
124587                  It was not clear,Did not Undersstand
124589                  Not Enough
125654                  Too Fast
124582                  Not frequent

逻辑是每当问题顺序是6或11然后我需要显示[问题部分标签]如果[问题部分标签]有一个Respondent_ID的多个值然后我需要连接它们但是当[问题的值]时Part Label]是Other(请注明):然后我需要使用Answer Text列中的值  一个

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

将其添加到“在此处替换代码”

 stuff((select ','+T2.[Question Part Label]   
        from Results as T2 
        where T1.[Respondent ID] = T2.[Respondent ID]    
        for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '') 

将T1添加为主查询中result表的别名。

答案 1 :(得分:1)

DECLARE @Results TABLE
(
    Respondent_ID INT,
    [Question Order] INT,
    [Question Part Label] VARCHAR(40),
    [Answer text] VARCHAR(80),
    [Answer Label] VARCHAR(10)
);

INSERT @Results
    SELECT       124587, 6, 'It was not clear',        NULL,         'Yes'
    UNION SELECT 124587, 6, 'Did not Undersstand',     NULL,         'Yes'
    UNION SELECT 124589, 6, 'Other (Please specify):', 'Not enough', 'Yes'
    UNION SELECT 124654, 6, 'Too Fast',                NULL,         'Yes'
    UNION SELECT 124582, 11, 'Not frequent',           NULL,         'Yes';

WITH x AS 
(
    SELECT Respondent_ID 
        FROM @Results 
        WHERE [Question Order] IN (6,11)
        GROUP BY Respondent_ID
)
SELECT x.Respondent_ID, Label = STUFF((SELECT ',' + CASE 
    WHEN [Question Part Label] = 'Other (Please specify):' THEN [Answer text] 
    ELSE [Question Part Label] END
    FROM @Results 
    WHERE [Question Order] IN (6,11)
    AND Respondent_ID = x.Respondent_ID
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '')
    FROM x;

答案 2 :(得分:0)

我不在使用SQL Server的计算机上,因此我无法对此进行测试(我确信您可以找到更有效的方法),但您可以尝试以下方法:

;WITH CTE AS
(
    SELECT  T1.[Respondent ID], 
            stuff(( SELECT ','+T2.[Question Part Label]
                    FROM Results as T2 
                    WHERE T1.[Respondent ID] = T2.[Respondent ID] 
                    FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '') as Label 
    FROM Results as T1 
    GROUP BY T1.[Respondent ID] 
)

SELECT  CASE WHEN [Question Order] IN ( 6, 11 )
             THEN CASE WHEN [Question Part Label] = 'Other (Please specify):'
                       THEN [Answer Text]
                       ELSE [Question Part Label]
                  END
             ELSE R2.Label
        END, R1.[Respondent ID]
FROM    Results R1
LEFT JOIN CTE R2
ON R1.[Respondent ID] = R2.[Respondent ID]
WHERE  [Question Order] = 6 AND [Answer Label] = 'Yes'