I am developing an application that allows configurable questions and answers. Currently there can be up to 20 answers, but possibly less.
The structure I have is as follows:
Questions
+----+--------+--------------+-------------+
| ID | FormId | QuestionText | AnswerField |
+----+--------+--------------+-------------+
| 1 | 1 | Name | Answer01 |
| 2 | 1 | Address | Answer02 |
| 3 | 1 | Phone | Answer03 |
| 4 | 1 | Email | Answer04 |
| 5 | 2 | First Name | Answer01 |
| 6 | 2 | Surname | Answer02 |
+----+--------+--------------+-------------+
Answers
+----+--------+----------+------------+--------------+--------------+----------------+----------+----------+
| ID | FormId | RecordId | Answer01 | Answer02 | Answer03 | Answer04 | Answer05 | Answer06 |
+----+--------+----------+------------+--------------+--------------+----------------+----------+----------+
| 1 | 1 | 1 | Bob Smith | Bobs Address | 01234 111222 | bob@smith.com | Null | Null |
| 2 | 1 | 2 | Joe Bloggs | Joes Address | 04321 333444 | joe@bloggs.com | Null | Null |
| 3 | 2 | 3 | David | Jones | Null | Null | Null | |
+----+--------+----------+------------+--------------+--------------+----------------+----------+----------+
So in the Questions table AnswerField Answer01 maps to the Answer01 column in the Answers table
What I would like to do is get a result set that looks something like:
For form ID 1 & record ID 1:
+--------------+---------------+
| QuestionText | Answer |
+--------------+---------------+
| Name | Bob Smith |
| Address | Bobs Address |
| Phone | 01234 111222 |
| Email | bob@smith.com |
+--------------+---------------+
Then for form id 2 & record id 3:
+--------------+---------+
| QuestionText | Answer |
+--------------+---------+
| First Name | David |
| Surname | Jones |
+--------------+---------+
I have tried using a pivot table:
SELECT QuestionText, Answer01, Answer02, Answer03, Answer04
FROM (
SELECT DISTINCT Q.AnswerField, Q.QuestionText, Q.ID, A.Answer01, A.Answer02, A.Answer03, A.Answer04
FROM Questions Q
INNER JOIN Answers A ON A.FormId= Q.FormId
WHERE A.ID = 17
)
AS src
PIVOT (MAX(question_id) FOR Answer IN(answer_01, answer_02, answer_03, answer_04)) AS pvt
But this repeats the answers in all columns:
+--------------+-----------+--------------+--------------+---------------+
| QuestionText | Answer01 | Answer02 | Answer03 | Answer04 |
+--------------+-----------+--------------+--------------+---------------+
| Name | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
| Address | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
| Phone | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
| Email | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
+--------------+-----------+--------------+--------------+---------------+
Which obviously isn't right.
Can anyone suggest how this might be done in a SQL Server stored procedure please?