我有一个超出我使用sql构建特定报告的经验的数据集,所以我正在寻找正确方向的指导。
数据
+-----+--------+------------+-----------------+
| ID | Name | QuestionID | Question Answer |
+-----+--------+------------+-----------------+
| 100 | Ethan | 3 | Null |
| 100 | Ethan | 4 | 4 p.m. |
| 100 | Ethan | 5 | no |
| 101 | Samuel | 3 | Null |
| 101 | Samuel | 4 | Null |
| 101 | Samuel | 5 | 10 a.m. |
| 103 | Faris | 3 | yes |
| 103 | faris | 5 | 8 a.m. |
+-----+--------+------------+-----------------+
我需要将输出构建到一个矩阵中,该矩阵列出每个ID一次,并将问题和答案分成列与行。
+-----+--------+-----------+---------+-----------+---------+-----------+---------+
| ID | Name | Question3 | Answer3 | Question4 | Answer4 | Question5 | Answer5 |
+-----+--------+-----------+---------+-----------+---------+-----------+---------+
| 100 | Ethan | 3 | Null | 4 | 4 p.m. | 5 | no |
| 101 | Samuel | 3 | Null | 4 | Null | 5 | 10 a.m. |
| 103 | Faris | 3 | yes | | | 5 | 8 a.m. |
+-----+--------+-----------+---------+-----------+---------+-----------+---------+
另一部分是,如果用户添加或存档问题,可以更改问题的数量。
我简化了数据,问题ID是对包含实际问题的单独表格的引用。