Hi, having some trouble trying to work out a query. I've already implemented this in code but would really like to do a single query for reporting purposes.Heres a cut down version of the tables:CREATE TABLE #Forms ( [Id] int NOT NULL IDENTITY(1, 1), [DateValidFrom] datetime NOT NULL, [DateValidTo] datetime NOT NULL ) INSERT INTO #Forms (DateValidFrom, DateValidTo) VALUES ('2013-01-01', '2014-01-01') CREATE TABLE #Questions ( [Id] int NOT NULL IDENTITY(1, 1), [QuestionText] nvarchar(999) NOT NULL ) INSERT INTO #Questions (QuestionText) VALUES ('Question 1?')INSERT INTO #Questions (QuestionText) VALUES ('Question 2?')INSERT INTO #Questions (QuestionText) VALUES ('Question 3?')INSERT INTO #Questions (QuestionText) VALUES ('Question 4?')INSERT INTO #Questions (QuestionText) VALUES ('Question 5?')INSERT INTO #Questions (QuestionText) VALUES ('Question 6?') CREATE TABLE #Headings ( [Id] int NOT NULL IDENTITY(1, 1), [HeadingText] nvarchar(100) NOT NULL ) INSERT INTO #Headings (HeadingText) VALUES ('Heading 1?')INSERT INTO #Headings (HeadingText) VALUES ('Heading 2?')INSERT INTO #Headings (HeadingText) VALUES ('Heading 3?') CREATE TABLE #FormQuestions ( [Id] int NOT NULL IDENTITY(1, 1), [QuestionId] int NOT NULL, [FormId] int NOT NULL, [HeadingId] int NOT NULL, [QuestionSortOrder] int NOT NULL, [HeadingSortOrder] int NOT NULL ) INSERT INTO #FormQuestions (QuestionId, FormId, HeadingId, QuestionSortOrder, HeadingSortOrder) VALUES (1, 1, 1, 1, 1)INSERT INTO #FormQuestions (QuestionId, FormId, HeadingId, QuestionSortOrder, HeadingSortOrder) VALUES (2, 1, 1, 2, 1)INSERT INTO #FormQuestions (QuestionId, FormId, HeadingId, QuestionSortOrder, HeadingSortOrder) VALUES (3, 1, 2, 1, 2)INSERT INTO #FormQuestions (QuestionId, FormId, HeadingId, QuestionSortOrder, HeadingSortOrder) VALUES (4, 1, 2, 2, 2)INSERT INTO #FormQuestions (QuestionId, FormId, HeadingId, QuestionSortOrder, HeadingSortOrder) VALUES (5, 1, 3, 1, 3)INSERT INTO #FormQuestions (QuestionId, FormId, HeadingId, QuestionSortOrder, HeadingSortOrder) VALUES (6, 1, 3, 2, 3)CREATE TABLE #FormInstance ( [Id] int NOT NULL IDENTITY(1, 1), [PersonName] nvarchar(50) NOT NULL, [PersonEmail] nvarchar(75) NOT NULL, [TimeSubmitted] datetime NOT NULL ) INSERT INTO #FormInstance (PersonName, PersonEmail, TimeSubmitted) VALUES ('Jack', 'jack@email.com', '2013-02-02')INSERT INTO #FormInstance (PersonName, PersonEmail, TimeSubmitted) VALUES ('Jill', 'jill@email.com', '2013-02-03') CREATE TABLE #InstanceQuestion ( [Id] int NOT NULL IDENTITY(1, 1), [FormInstanceId] int NOT NULL, [FormQuestionId] int NOT NULL, [Response] nvarchar(4000) ) INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (1, 1, 'blah blah blah')INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (1, 2, 'blah blah blah')INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (1, 3, NULL)INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (1, 4, NULL)INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (1, 5, 'blah blah blah')INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (1, 6, NULL)INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (2, 1, NULL)INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (2, 2, 'blah blah blah')INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (2, 3, 'blah blah blah')INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (2, 4, 'blah blah blah')INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (2, 5, NULL)INSERT INTO #InstanceQuestion (FormInstanceId, FormQuestionId, Response) VALUES (2, 6, NULL)
This data structure is for the creation of custom forms. Admin users add questions and headings, then create a form and add questions and headings to the form. So the Forms table is a form and the FormQuestions hold the headings and questions for that form. Then once a user fills out a form, a FormInstance is created and the answers are stored in the InstanceQuestions. So in the above code to create the tables, one form has been created. The form has 3 headings that contain 2 questions under each heading. So the form has 3 headings and 6 questions.2 Users have filled out a form, Jack and Jill so there are 2 FormInstances. And their answers are stored in the InstanceQuestion table, so 2 forms filled out, 6 questions a form so 12 InstanceQuestions.So, now on to the report. I want to report on responses in the InstanceQuestion table that are not null. I want to count each not null response by heading. And the form headings as columns. So for example the report would show that jack filled out a form and for heading 1 (as a column) there were 2 not null responses (in the row), for heading 2 (column) there were 0 not null responses (row) and so on (join InstanceQuestions with FormQuestions to get the Heading).Is this possible with SQL? If there is anything that needs to be made clearer, let me know.Thanks,Gareth