Reporting services SSRS-矩阵中的重复值
我是这方面的初学者,请原谅我的无知。我已经搜索了几个小时,但还没有经验来更好地了解到底要搜索什么 我在SSRS中有如下数据集:Reporting services SSRS-矩阵中的重复值,reporting-services,visual-studio-2015,Reporting Services,Visual Studio 2015,我是这方面的初学者,请原谅我的无知。我已经搜索了几个小时,但还没有经验来更好地了解到底要搜索什么 我在SSRS中有如下数据集: |---------------------|------------------| | Subject | Student | |---------------------|------------------| | Biology | Student A
|---------------------|------------------|
| Subject | Student |
|---------------------|------------------|
| Biology | Student A |
|---------------------|------------------|
| Chemistry | Student A |
|---------------------|------------------|
| Chemistry | Student B |
|---------------------|------------------|
| Physics | Student B |
|---------------------|------------------|
我想写一个矩阵,统计所有同时学习化学和物理等学科的学生。目前,我的矩阵只计算单个学科的学生人数,结果如下:
|---------------------|------------------|------------------|------------------|
| | Biology | Chemistry | Physics |
|---------------------|------------------|------------------|------------------|
| Biology | 20 | 0 | 0 |
|---------------------|------------------|------------------|------------------|
| Chemistry | 0 | 36 | 0 |
|---------------------|------------------|------------------|------------------|
| Physics | 0 | 0 | 16 |
|---------------------|------------------|------------------|------------------|
我遗漏了什么?首先,您的数据集需要将主题合并为Subject1和Subject2,例如,通过在Student字段中将表连接到自身
然后,在矩阵中,使用按Subject1分组的行组和按Subject2分组的列组。首先,您的数据集需要将主题合并为Subject1和Subject2,例如,通过在Student字段中将表连接到自身 然后,在矩阵中,使用按主题1分组的行组和按主题2分组的列组。这很简单: 您可以将学生和主题之间的地图与其自身连接起来,但前提是学生相同,但主题不同 然后在报告中对studentId进行计数,不管它是来自map1还是map2,因为它总是相同的
;WITH subjects AS
(
SELECT 1 AS subject_id, 'Biology' AS subject_name
UNION ALL SELECT 2 AS subject_id, 'Chemistry' AS subject_name
UNION ALL SELECT 3 AS subject_id, 'Physics' AS subject_name
)
,students AS
(
SELECT 1 AS student_id, 'Student A' AS student_name
UNION ALL SELECT 2 AS student_id, 'Student B' AS student_name
UNION ALL SELECT 3 AS student_id, 'Student C' AS student_name
UNION ALL SELECT 4 AS student_id, 'Student D' AS student_name
)
,map_student_subject AS
(
SELECT 1 AS student_id, 1 AS subject_id
UNION ALL SELECT 1 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 3 AS subject_id
UNION ALL SELECT 3 AS student_id, 3 AS subject_id
UNION ALL SELECT 4 AS student_id, 3 AS subject_id
)
SELECT
map1.student_id AS map1_student_id
,map1_student.student_name AS map1_student_name
,map1.subject_id AS map1_subject_id
,map1_subject.subject_name AS map1_subject_name
,map2.student_id AS map2_student_id
,map2_student.student_name AS map2_student_name
,map2.subject_id AS map2_subject_id
,map2_subject.subject_name AS map2_subject_name
FROM map_student_subject AS map1
INNER JOIN map_student_subject AS map2
ON map2.student_id = map1.student_id
AND map2.subject_id <> map1.subject_id
LEFT JOIN students AS map1_student
ON map1_student.student_id = map1.student_id
LEFT JOIN students AS map2_student
ON map2_student.student_id = map2.student_id
LEFT JOIN subjects AS map1_subject
ON map1_subject.subject_id = map1.subject_id
LEFT JOIN subjects AS map2_subject
ON map2_subject.subject_id = map2.subject_id
结果:
如果你想列出那些只有一门学科的学生,你需要建立一个联盟,因为只要去掉这个联盟,就不可能计算出一门学科的实际学生人数:
;WITH subjects AS
(
SELECT 1 AS subject_id, 'Biology' AS subject_name
UNION ALL SELECT 2 AS subject_id, 'Chemistry' AS subject_name
UNION ALL SELECT 3 AS subject_id, 'Physics' AS subject_name
)
,students AS
(
SELECT 1 AS student_id, 'Student A' AS student_name
UNION ALL SELECT 2 AS student_id, 'Student B' AS student_name
UNION ALL SELECT 3 AS student_id, 'Student C' AS student_name
UNION ALL SELECT 4 AS student_id, 'Student D' AS student_name
)
,map_student_subject AS
(
SELECT 1 AS student_id, 1 AS subject_id
UNION ALL SELECT 1 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 3 AS subject_id
UNION ALL SELECT 3 AS student_id, 3 AS subject_id
UNION ALL SELECT 4 AS student_id, 3 AS subject_id
)
SELECT
map1.student_id AS map1_student_id
,map1_student.student_name AS map1_student_name
,map1.subject_id AS map1_subject_id
,map1_subject.subject_name AS map1_subject_name
,map2.student_id AS map2_student_id
,map2_student.student_name AS map2_student_name
,map2.subject_id AS map2_subject_id
,map2_subject.subject_name AS map2_subject_name
FROM map_student_subject AS map1
INNER JOIN map_student_subject AS map2
ON map2.student_id = map1.student_id
AND map2.subject_id <> map1.subject_id
LEFT JOIN students AS map1_student
ON map1_student.student_id = map1.student_id
LEFT JOIN students AS map2_student
ON map2_student.student_id = map2.student_id
LEFT JOIN subjects AS map1_subject
ON map1_subject.subject_id = map1.subject_id
LEFT JOIN subjects AS map2_subject
ON map2_subject.subject_id = map2.subject_id
UNION ALL
SELECT
students.student_id AS map1_student_id
,students.student_name AS map1_student_name
,MAX(subjects.subject_id) AS map1_subject_id
,MAX(subjects.subject_name) AS map1_subject_name
,students.student_id AS map2_student_id
,students.student_name AS map2_student_name
,MAX(subjects.subject_id) AS map2_subject_id
,MAX(subjects.subject_name) AS map2_subject_name
FROM map_student_subject
LEFT JOIN students ON students.student_id = map_student_subject.student_id
LEFT JOIN subjects ON subjects.subject_id = map_student_subject.subject_id
GROUP BY
students.student_id
,students.student_name
HAVING COUNT(*) = 1
这很简单:
您可以将学生和主题之间的地图与其自身连接起来,但前提是学生相同,但主题不同
然后在报告中对studentId进行计数,不管它是来自map1还是map2,因为它总是相同的
;WITH subjects AS
(
SELECT 1 AS subject_id, 'Biology' AS subject_name
UNION ALL SELECT 2 AS subject_id, 'Chemistry' AS subject_name
UNION ALL SELECT 3 AS subject_id, 'Physics' AS subject_name
)
,students AS
(
SELECT 1 AS student_id, 'Student A' AS student_name
UNION ALL SELECT 2 AS student_id, 'Student B' AS student_name
UNION ALL SELECT 3 AS student_id, 'Student C' AS student_name
UNION ALL SELECT 4 AS student_id, 'Student D' AS student_name
)
,map_student_subject AS
(
SELECT 1 AS student_id, 1 AS subject_id
UNION ALL SELECT 1 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 3 AS subject_id
UNION ALL SELECT 3 AS student_id, 3 AS subject_id
UNION ALL SELECT 4 AS student_id, 3 AS subject_id
)
SELECT
map1.student_id AS map1_student_id
,map1_student.student_name AS map1_student_name
,map1.subject_id AS map1_subject_id
,map1_subject.subject_name AS map1_subject_name
,map2.student_id AS map2_student_id
,map2_student.student_name AS map2_student_name
,map2.subject_id AS map2_subject_id
,map2_subject.subject_name AS map2_subject_name
FROM map_student_subject AS map1
INNER JOIN map_student_subject AS map2
ON map2.student_id = map1.student_id
AND map2.subject_id <> map1.subject_id
LEFT JOIN students AS map1_student
ON map1_student.student_id = map1.student_id
LEFT JOIN students AS map2_student
ON map2_student.student_id = map2.student_id
LEFT JOIN subjects AS map1_subject
ON map1_subject.subject_id = map1.subject_id
LEFT JOIN subjects AS map2_subject
ON map2_subject.subject_id = map2.subject_id
结果:
如果你想列出那些只有一门学科的学生,你需要建立一个联盟,因为只要去掉这个联盟,就不可能计算出一门学科的实际学生人数:
;WITH subjects AS
(
SELECT 1 AS subject_id, 'Biology' AS subject_name
UNION ALL SELECT 2 AS subject_id, 'Chemistry' AS subject_name
UNION ALL SELECT 3 AS subject_id, 'Physics' AS subject_name
)
,students AS
(
SELECT 1 AS student_id, 'Student A' AS student_name
UNION ALL SELECT 2 AS student_id, 'Student B' AS student_name
UNION ALL SELECT 3 AS student_id, 'Student C' AS student_name
UNION ALL SELECT 4 AS student_id, 'Student D' AS student_name
)
,map_student_subject AS
(
SELECT 1 AS student_id, 1 AS subject_id
UNION ALL SELECT 1 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 2 AS subject_id
UNION ALL SELECT 2 AS student_id, 3 AS subject_id
UNION ALL SELECT 3 AS student_id, 3 AS subject_id
UNION ALL SELECT 4 AS student_id, 3 AS subject_id
)
SELECT
map1.student_id AS map1_student_id
,map1_student.student_name AS map1_student_name
,map1.subject_id AS map1_subject_id
,map1_subject.subject_name AS map1_subject_name
,map2.student_id AS map2_student_id
,map2_student.student_name AS map2_student_name
,map2.subject_id AS map2_subject_id
,map2_subject.subject_name AS map2_subject_name
FROM map_student_subject AS map1
INNER JOIN map_student_subject AS map2
ON map2.student_id = map1.student_id
AND map2.subject_id <> map1.subject_id
LEFT JOIN students AS map1_student
ON map1_student.student_id = map1.student_id
LEFT JOIN students AS map2_student
ON map2_student.student_id = map2.student_id
LEFT JOIN subjects AS map1_subject
ON map1_subject.subject_id = map1.subject_id
LEFT JOIN subjects AS map2_subject
ON map2_subject.subject_id = map2.subject_id
UNION ALL
SELECT
students.student_id AS map1_student_id
,students.student_name AS map1_student_name
,MAX(subjects.subject_id) AS map1_subject_id
,MAX(subjects.subject_name) AS map1_subject_name
,students.student_id AS map2_student_id
,students.student_name AS map2_student_name
,MAX(subjects.subject_id) AS map2_subject_id
,MAX(subjects.subject_name) AS map2_subject_name
FROM map_student_subject
LEFT JOIN students ON students.student_id = map_student_subject.student_id
LEFT JOIN subjects ON subjects.subject_id = map_student_subject.subject_id
GROUP BY
students.student_id
,students.student_name
HAVING COUNT(*) = 1
您能否分享一些关于当前设计和分组的更多信息,以获得该结果?设计视图的一些屏幕截图/图像可能非常有用。您能否分享一些关于当前设计的更多信息,以及用于获得该结果的分组?设计视图的一些截图/图像可能非常有用。