Let's suppose I have two tables:
Question (Id, Text)
Answer (Value, QuestionId, Guid)
Column Guid
groups answers from the same person.
I need a query to produce results like this:
'Question 1' | 'Question 2'
4 | 3
1 | NULL
NULL | 5
2 | 6
9 | NULL
Questions texts are transformed into column headers and answers values are in rows. Answers are grouped by Guid
, so there are answers from one person in one row. If a person didn't answer particular question, NULL is returned.
Number of questions can vary.
Data used to produce sample results:
Question
Id | Text
1 | Question 1
2 | Question 2
Answer
Value | QuestionId | Guid
4 | 1 | AAA
3 | 2 | AAA
1 | 1 | BBB
5 | 2 | CCC
2 | 1 | DDD
6 | 2 | DDD
9 | 1 | EEE
Can you please help me out with a query to produce the results?