SSRS将多列中的多行合并为一行

问题描述 投票:0回答:2

我们需要检查学生考试的要求(例如,分配的额外时间(A),文字处理程序(B),阅读器(C)等)。当前,SQL抽取出如下信息:

Student | Exam Requirements
---------------------------
   1    |        A
   1    |        B
   2    |        A
   2    |        B
   2    |        C
   3    |        B
   4    |        B
   4    |        C
   5    |        A
   6    |        D
   7    |        E
   8    |        F

而且我需要像这样在SSRS中显示它:

学生|要求A |要求B |要求C |其他要求

   1    |       Y       |       Y       |       -       |       -        
   2    |       Y       |       Y       |       Y       |       -        
   3    |       -       |       Y       |       -       |       -        
   4    |       -       |       Y       |       Y       |       -       
   5    |       Y       |       -       |       -       |       -       
   6    |       -       |       -       |       -       |       D       
   7    |       -       |       -       |       -       |       E       
   8    |       -       |       -       |       -       |       F        

当前,由于行组是按学生分组的,因此我的表仅在要求A的列中显示'Y'。如果我按要求将其分组,则它会创建与学生要求相同的行。

谢谢,罗布

sql reporting-services group-by visual-studio-2008 pivot
2个回答
1
投票

使用条件聚合:

select 
    student,
    coalesce(max(case when exam_requirements = 'A' then 'Y' end), '-') requirement_a,
    coalesce(max(case when exam_requirements = 'B' then 'Y' end), '-') requirement_b,
    coalesce(max(case when exam_requirements = 'C' then 'Y' end), '-') requirement_c,
    coalesce(max(case when exam_requirements not in ('A', 'B', 'C') then exam_requirements end), '-') other_requirement
from mytable
group by student

0
投票

我通过以下查询重新创建了数据集

DECLARE @t TABLE(Student int, Requirement varchar(10))
INSERT INTO @t VALUES 
(1, 'A'),(1, 'B'),(2, 'A'),(2, 'B'),(2, 'C'),(3, 'B'),(4, 'B'),(4, 'C'),(5, 'A'),(6, 'D'),(7, 'E'),(8, 'F')

然后在报告中使用此数据集

SELECT 
    Student
    , CASE WHEN Requirement IN ('A','B','C') THEN Requirement ELSE 'Other' END AS ExamRequirement
    , CASE WHEN Requirement IN ('A','B','C') THEN 'Y' ELSE Requirement END AS reportvalue
    FROM @t

这将提供以下输出

enter image description here

然后我在报表中添加一个矩阵,通过Student设置行组,通过ExamRequirement设置列组,将[数据]设置为reportValue。设计看起来像这样

enter image description here

最终输出看起来像这样

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.