I have three tables - the first contains questions with a unique ID
QID | Question
------------------
1 | Question 1
2 | Question 2
3 | Question 3
4 | Question 4
5 | Question 5
The second contains answers to those questions, the unique ID from the first table and then an ID of the record that links the answers together
QID | Answer | Parent
-------------------------
1 | Yes | 123
2 | No | 123
3 | No | 123
4 | Yes | 123
5 | Yes | 123
1 | No | 124
2 | Yes | 124
3 | No | 124
4 | No | 124
5 | No | 124
The third contains other information about the parent
Parent | Parent Title
----------------------------------------
123 | Questionnaire Sample 1
124 | Questionnaire Sample 2
Ideally I would like to create a view that transposes or pivots the data into a more usable format, I'm thinking something along the lines of:
Parent | Question 1 | Question 2 | Question 3 | Question 4 | Question 5
123 | Yes | No | No | Yes | Yes
124 | No | Yes | No | No | No
I found a couple of really nice threads on here that I've been working with:
Simple way to transpose columns and rows in Sql?
T-SQL Pivot/Unpivot(Transpose) Column Headers Needed as Data Rows
There was another one:
Transpose rows into columns in SQL Server 2008 R2
that I pulled the following from:
SELECT [First_Set], [Second_Set]
FROM
(
SELECT B.ColumnName, A.Value
, row_number() over(partition by a.Table2ID
order by a.Value) seq
FROM Table_1 AS A
INNER JOIN Table_2 AS B
ON A.Table2ID = B.ID
) AS P
PIVOT
(
min(P.Value)
for P.ColumnName in ([First_Set], [Second_Set])
) AS PIV;
I then modified that to be:
SELECT [PARENT], [QUESTION], [ANSWER]
FROM
(
SELECT B.PARENT_TITLE, C.QUESTION, A.ANSWER
, row_number() over(partition by a.PARENT
order by a.PARENT) seq
FROM answerTable AS A
INNER JOIN parentTable AS B
ON A.PARENT = B.PARENT
INNER JOIN questionTable AS C
ON A.QID = C.QID
) AS P
PIVOT
(
min(P.RESULT)
for P.PARENT in ([PARENT], [QUESTION], [ANSWER])
) AS PIV;
Which returns but still isn't as I need it.
It's not feasible for me to create new tables so ideally I'm looking for a dynamic means here, it's for reporting so a view would be easiest / best in my opinion but I am open to suggestions.
Thanks.