Using SQL Server 2000
I'm trying to perform a series of joins to get multiple values out of a single set of tables. The purpose of this is to get a single view of all responses to a single responseID. The data is stored as:
...and I want the following result:
I can't seem to get this right; I'm trying to leave the NULLs in place, but the problem is that the ResponseAnswers table doesn't store a NULL response for that question. Using the query I wrote, the only rows that would return would be 1 and 2, since none of the values can be NULL:
Any ideas? Help! Thanks!
I'm trying to perform a series of joins to get multiple values out of a single set of tables. The purpose of this is to get a single view of all responses to a single responseID. The data is stored as:
Code:
ResponseAnswer
ResponseID[tab]ItemID[tab]OptionID
1 [tab]500 [tab]10
1 [tab]501 [tab]25
2 [tab]500 [tab]11
2 [tab]501 [tab]24
3 [tab]501 [tab]23
4 [tab]500 [tab]10
ItemOptions
OptionID[tab]TextID
10 [tab]100
11 [tab]110
23 [tab]230
24 [tab]240
25 [tab]250
Text
TextID[tab]TextValue
100 [tab]'Y'
110 [tab]'N'
230 [tab]'3'
240 [tab]'4'
250 [tab]'5'
...and I want the following result:
Code:
ResponseID[tab]responseQ1[tab]responseQ2[tab]...[tab]ResponseQn
1 [tab]Y [tab]5 [tab]...[tab]value
2 [tab]N [tab]4 [tab]...[tab]value
3 [tab]NULL [tab]3 [tab]...[tab]value
4 [tab]Y [tab]NULL [tab]...[tab]value
...
I can't seem to get this right; I'm trying to leave the NULLs in place, but the problem is that the ResponseAnswers table doesn't store a NULL response for that question. Using the query I wrote, the only rows that would return would be 1 and 2, since none of the values can be NULL:
Code:
select response.responseID, response.Ended as responseDate, 'I' as responseType, LText.TextValue as responseLoc,
Text_1.TextValue as responseSvcDate, Text_2.TextValue as responsePC, Text_3.TextValue as responseQ1,
Text_4.TextValue as responseQ2, Text_5.TextValue as responseQ3
from response
left outer join responseAnswers responseAnswers on response.responseID = responseAnswers.responseID
left outer join ItemOptions ItemOptions on responseAnswers.optionID = ItemOptions.optionID
left outer join Text LText on ItemOptions.TextID = LText.TextID
left outer join responseAnswers responseAnswers_1 on response.responseID = responseAnswers_1.responseID
left outer join ItemOptions ItemOptions_1 on responseAnswers_1.optionID = ItemOptions_1.optionID
left outer join Text Text_1 on ItemOptions_1.TextID = Text_1.TextID
left outer join responseAnswers responseAnswers_2 on response.responseID = responseAnswers_2.responseID
left outer join ItemOptions ItemOptions_2 on responseAnswers_2.optionID = ItemOptions_2.optionID
left outer join Text Text_2 on ItemOptions_2.TextID = Text_2.TextID
left outer join responseAnswers responseAnswers_3 on response.responseID = responseAnswers_3.responseID
left outer join ItemOptions ItemOptions_3 on responseAnswers_3.optionID = ItemOptions_3.optionID
left outer join Text Text_3 on ItemOptions_3.TextID = Text_3.TextID
left outer join responseAnswers responseAnswers_4 on response.responseID = responseAnswers_4.responseID
left outer join ItemOptions ItemOptions_4 on responseAnswers_4.optionID = ItemOptions_4.optionID
left outer join Text Text_4 on ItemOptions_4.TextID = Text_4.TextID
left outer join responseAnswers responseAnswers_5 on response.responseID = responseAnswers_5.responseID
left outer join ItemOptions ItemOptions_5 on responseAnswers_5.optionID = ItemOptions_5.optionID
where (responseAnswers.itemID = 22839) and (responseAnswers_1.itemID = 22832)
and (responseAnswers_2.itemID = 22838) and (responseAnswers_3.itemID = 22840)
and (responseAnswers_4.itemID = 22837) and (responseAnswers_5.itemID = 22836)
Any ideas? Help! Thanks!