简体   繁体   English

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

[英]SSRS combining multiple rows from multiple columns into one row

We need to check requirements for students exams (eg extra time allocated (A), word processor (B), reader (C), and other).我们需要检查学生考试的要求(例如分配的额外时间 (A)、文字处理器 (B)、阅读器 (C) 等)。 Currently the SQL pumps out the information like this:目前,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

and I need to display it in SSRS like this:我需要像这样在 SSRS 中显示它:

Student |学生 | Requirement A |要求 A | Requirement B |要求 B | Requirement C |要求 C | Other requirement其他要求

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

Currently, as the row group is grouped on the Student, my table only shows a 'Y' in the column for Requirement A. If I group it on requirement then it creates as many rows as the student has requirements.目前,由于行组在学生上分组,我的表格只在要求 A 的列中显示一个“Y”。如果我按要求对其进行分组,那么它会创建与学生有要求一样多的行。

Thanks, Rob谢谢,罗布

Use conditional aggregation:使用条件聚合:

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

I recreated the dataset with the following query我使用以下查询重新创建了数据集

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')

Then used this dataset in the report然后在报表中使用这个数据集

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

This gives the following output这给出了以下输出

在此处输入图片说明

I then added a Matrix to the report, set the row group by Student and the column group by ExamRequirement and the [data] to reportValue.然后我在报告中添加了一个矩阵,将行组设置为 Student,将列组设置为 ExamRequirement,将 [data] 设置为 reportValue。 The design looks like this设计看起来像这样

在此处输入图片说明

The final output looks like this最终输出看起来像这样

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM