我有两个表Question和Person。人员进行调查后,人员和问题将输入到人员调查表中。
问题
问题1选项1选项2选项3选项4
问题2选项1选项2选项3选项4
个人调查表
Person1 Question1 Option2
Person1 Question2 Option3
Person3 Question1 Option4
Person2 Question1 Option1
Person2 Question2 Option2
Person3 Question2 Option3
我需要在SSRS中创建一个矩阵报告,如下所示:
报告
问题1选项1选项2选项3选项4
1 1 0 1
问题2选项1选项2选项3选项4
0 1 2 0
我的问题是:即使每个人只选择了一些选项,我如何获得每个问题的所有选项?
谢谢。
答案 0 :(得分:1)
您需要创建一个查询,将所有问题与您的问题进行联接,然后将您的PersonSurvey表与该查询的结果保持联接,并在PersonSurvey中对Options进行计数。
不幸的是,您的问题数据并非全部集中在一个字段中,因此需要将其删除。
有点难以解释,所以这是我使用您的数据创建的示例。
SELECT 1 AS QUESTION_ID, '1Answer1' AS OPTION1, '1Answer2' AS OPTION2, '1Answer3' AS OPTION3, '1Answer4' AS OPTION4
INTO #QUESTION
UNION
SELECT 2 AS QUESTION_ID, '2Answer1' AS OPTION1, '2Answer2' AS OPTION2, '2Answer3' AS OPTION3, '2Answer4' AS OPTION4
UNION
SELECT 3 AS QUESTION_ID, '3Answer1' AS OPTION1, '3Answer2' AS OPTION2, '3Answer3' AS OPTION3, '3Answer4' AS OPTION4
SELECT 1 AS PERSON_ID, 1 AS QUESTION_ID, '1Answer2' AS ANSWER
INTO #PERSONSURVEY
UNION
SELECT 1 AS PERSON_ID, 2 AS QUESTION_ID, '2Answer3' AS ANSWER
UNION
SELECT 2 AS PERSON_ID, 1 AS QUESTION_ID, '1Answer1' AS ANSWER
UNION
SELECT 2 AS PERSON_ID, 2 AS QUESTION_ID, '2Answer2' AS ANSWER
UNION
SELECT 3 AS PERSON_ID, 1 AS QUESTION_ID, '1Answer4' AS ANSWER
UNION
SELECT 3 AS PERSON_ID, 2 AS QUESTION_ID, '2Answer3' AS ANSWER
;WITH QUESTIONS AS (
SELECT QUESTION_ID, OPTIONS, ANSWERS
FROM #QUESTION
UNPIVOT
(ANSWERS FOR OPTIONS IN (OPTION1, OPTION2, OPTION3, OPTION4 )) AS UNPVT
)
SELECT Q.QUESTION_ID, Q.OPTIONS, Q.ANSWERS, COUNT(P.ANSWER) AS ANSWERS
FROM QUESTIONS Q
LEFT JOIN #PERSONSURVEY P ON P.QUESTION_ID = Q.QUESTION_ID AND P.ANSWER = Q.ANSWERS
GROUP BY Q.QUESTION_ID, Q.OPTIONS, Q.ANSWERS
结果表与您提供的值匹配:
第一季度-1 1 0 1
第二季度-0 1 2 0
现在,您可以将问题作为行组,将选项作为列分组来创建矩阵。