【问题标题】:Group rows by ID in pivot query在数据透视查询中按 ID 对行进行分组
【发布时间】:2019-10-02 19:01:36
【问题描述】:

我正在尝试创建一个透视查询来显示数据库中的调查响应数据。

这是我目前的查询:

declare
    @columns     nvarchar(max) ='',
    @sql         nvarchar(max) = '',
    @responseIDs nvarchar(max) = '';

select
    @columns+=QUOTENAME(QuestionLabel) + ','
from
    SurveyQuestions
where
    SurveyID=1
order by
    QuestionSort;

set @columns = LEFT(@columns, LEN(@columns)-1);
set @responseIDs = (select ResponseIDs from SurveyCart where SurveyCartID=7);

set @sql='
select * from ( 
    select 
        QuestionLabel, ResponseAnswer, ResponseID, AmountPaid = 
        (
            IIF(QuestionTypeID=2 and ResponseAnswer=''Yes'', QuestionValue, 0) +
            IIF(QuestionTypeID=6 and CAST(ResponseAnswer as int)>0, QuestionValue*Cast(ResponseAnswer as int), 0)
        )
    from 
        SurveyQuestions q
        inner join SurveyResponseAnswers ra 
            on q.QuestionID=ra.QuestionID
    where
        ResponseID in (' + @responseIDs + ')
) t
pivot(
    MAX(ResponseAnswer) for QuestionLabel in (' + @columns + ')
) as pivot_table;';

EXECUTE(@sql)

这是我得到的结果

ResponseID  AmountPaid  Attending?  Select     Cost with Quantities
19          0.00            NULL    Option 3   NULL
20          0.00            NULL    Option 1    
19          25.00           Yes     NULL       NULL
20          25.00           Yes     NULL       NULL
19          30.00           NULL    NULL       3

我想要的结果:

ResponseID  AmountPaid  Attending?  Select     Cost with Quantities
19          55.00           Yes     Option 3   3
20          25.00           Yes     Option 1    

我希望每个响应 ID 只有一行,并且在 Attinging 和 Cost With quantity 中有空白点我需要底部行的结果和要根据 ResponseID 相加的金额。

【问题讨论】:

  • 这将有助于实际数据插入以进行测试,以及您想要的实际输出。但鉴于您到目前为止的描述,您可能必须使用 GROUP BY ResponseID 并对金额求和,其他列上使用 MIN/MAX。
  • 我认为您只需摆脱枢轴并使用简单的group by。您已经对QuestionTypeId 值进行了硬编码,是否真的需要使ResponseIDs 动态化。无论哪种方式,只需使用 case 或 if 包装列表达式,就像您已经做过的那样。
  • 我需要数据透视表,因为我正在从 QuestionLabels 中创建列,这些列与 Responses 位于不同的表中

标签: sql sql-server pivot


【解决方案1】:
select 
    ResponseID,
    sum(case when QuestionLabel = 'AmountPaid' then
        iif(QuestionTypeID = 2 and ResponseAnswer = 'Yes', QuestionValue, 0) +
        iif(QuestionTypeID = 6 and cast(ResponseAnswer as int) > 0,
            QuestionValue * Cast(ResponseAnswer as int), 0)
    end) as "AmountPaid",
    min(case when QuestionLabel = 'Attending?'
        then ResponseAnswer end) as "Attending?",
    min(case when QuestionLabel = 'Select'
        then ResponseAnswer end) as "Select",
    min(case when QuestionLabel = 'Cost with quantities'
        then ResponseAnswer end) as "Cost with Quantities"
from 
    SurveyQuestions q
    inner join SurveyResponseAnswers ra 
        on q.QuestionID = ra.QuestionID
where ResponseID in (
    select ResponseIDs from SurveyCart where SurveyCartID = 7
);

我不完全理解什么是“问题价值”或“调查车”,但我认为这应该很接近。除非您期望问题标签在未来发生变化,否则我不明白为什么您不能在这里硬编码它们,因为您已经使用了许多 ID 值。

【讨论】:

  • 问题标签会有所不同,因为用户可以使用任何问题进行自己的调查和表格。这就是为什么它必须是动态的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
相关资源
最近更新 更多