I got 3 tables as follow:
Table 1: FormFields (FieldID, FormID, Title, DateModified, ...)
Table 2: ReportItems (ReportItemID, ReportID, FormID, Title, DateModified, ...)
Table 3: FormValues (ValueID, FormID, FieldID, Value, UserName, DateModified, ...)
For example I have selected 2 Fields of FormFields as my ReportItems. These 2 fields are a question and answer couple. Each question can have 1 answer or it could be NULL. The key is the result should be such a html data list (dl) with 'dt's and 'dd's like this:
ReportID FormID FieldID Value (Question1) UserName
ReportID FormID FieldID Value (Answer1) UserName
ReportID FormID FieldID Value (Question2) UserName
ReportID FormID FieldID Value (Answer2) UserName
ReportID FormID FieldID Value (Question3) UserName
ReportID FormID FieldID Value (Answer3) UserName
Question 1
Answer 1
Question 2
Question 3
Answer 3
Question 4
Question 5
Answer 5
I used a query like this, but it doesn't return the result I wish:
select rf.FormID, rf.FieldID, rf.ReportItemTitle, v.Value from
(select r.ReportItemID, r.FormID, r.FieldID, r.Title as ReportItemTitle from
(select ReportItemID, ReportID, FormID, FieldID, Title
from ReportItems
where FormID = '2D6C42E1-0C95-4E40-B792-A17E00C001BF' and ShowInList = 0) r
JOIN
(select FormID, FieldID, Title
from FormFields
where FormID = '2D6C42E1-0C95-4E40-B792-A17E00C001BF') f
ON r.FormID = f.FormID AND r.FieldID = f.FieldID) rf
LEFT JOIN
(select FormID, FieldID, Value
from FormValues
where FormID = '2D6C42E1-0C95-4E40-B792-A17E00C001BF' and UserName = '0000000000') v
ON rf.FormID = v.FormID AND rf.FieldID = v.FieldID
FormID FieldID ReportItem's Title Values
2D6C42E1-0C95-4E40-B792-A17E00C001BF E7A78AC7-10A4-4752-85D6-A17E00C22EF9 Question's Title user's 1st question goes here...
2D6C42E1-0C95-4E40-B792-A17E00C001BF E7A78AC7-10A4-4752-85D6-A17E00C22EF9 Question's Title user's 2nd question goes here...
2D6C42E1-0C95-4E40-B792-A17E00C001BF E7A78AC7-10A4-4752-85D6-A17E00C22EF9 Question's Title user's 2nd question goes here...
2D6C42E1-0C95-4E40-B792-A17E00C001BF 8E1FAC2A-02F1-4D0D-A3E0-A184001484D8 Answer's Title admin's answer to user's 1st question
Note:
- Empty answers are the one has not been answered (NULL).
- The result should be based upon UserName and ReportID
- All IDs are GUID
- Both questions and answers are stored in FormValues table
Maybe I should use cursor, but I'm really confused what to do. I'd highly appreciate if someone help me.
Many thanks in advance,
Kardo