Hello.
I'm sure this must be a FAQ but can't find it anywhere.
I'm collecting data from people in a questionnaire style but don't know what/how many questions there are. I need to create a view that displays responses to each of the questions as a view, as follows:
Name | Company | Favourite Colour | Age Range | Favourite Food
In this example, there are three 'dynamic' questions. The data is stored as follows:
- People - basic information about the respondents
- Questions - the names of the questions being asked
- Choices - the options that can be chosen from for each question
- Responses - the answers that people provide.
If I know exactly what questions there are, then I can JOIN to the questions table the appropriate number of times. But I don't! Can this be done without dynamic SQL?
Very many thanks for help.
Simplified tables and data follow:
CREATE TABLE [People]( [ID] [int] NULL, [Name] [varchar](50) NULL, [Company] [varchar](50) NULL ) ON [PRIMARY] GO CREATE TABLE [Questions]( [ID] [int] NULL, [QuestionName] [varchar](50) NULL ) ON [PRIMARY] GO CREATE TABLE [Choices]( [ID] [int] NULL, [QuestionID] [int] NULL, [ChoiceName] [varchar](50) NULL ) ON [PRIMARY] GO CREATE TABLE [Responses]( [ID] [int] NULL, [PersonID] [int] NULL, [QuestionID] [int] NULL, [ChoiceID] [int] NULL, [OtherText] [varchar](50) NULL ) ON [PRIMARY] GO INSERT [People] ([ID], [Name], [Company]) VALUES (1, N'Ben', N'ACME') GO INSERT [People] ([ID], [Name], [Company]) VALUES (2, N'Dave', N'My Corp') GO INSERT [People] ([ID], [Name], [Company]) VALUES (3, N'Sarah', N'Newco') GO INSERT [Questions] ([ID], [QuestionName]) VALUES (1, N'FavouriteColour') GO INSERT [Questions] ([ID], [QuestionName]) VALUES (2, N'AgeRange') GO INSERT [Questions] ([ID], [QuestionName]) VALUES (3, N'FavouriteFood') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (1, 1, N'Red') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (2, 1, N'Blue') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (3, 1, N'Green') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (4, 2, N'<18') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (5, 2, N'18-65') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (6, 2, N'65+') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (7, 3, N'Pasta') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (8, 3, N'Curry') GO INSERT [Choices] ([ID], [QuestionID], [ChoiceName]) VALUES (9, 3, N'Steak') GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (1, 1, 1, 1, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (2, 1, 2, 1, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (3, 1, 3, 2, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (4, 2, 1, 4, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (5, 2, 2, 5, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (6, 2, 3, 6, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (7, 3, 1, 7, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (8, 3, 2, 9, NULL) GO INSERT [Responses] ([ID], [PersonID], [QuestionID], [ChoiceID], [OtherText]) VALUES (9, 3, 3, 9, NULL) GO