|
I am building a surveying system and have come across a problem when trying to get the aggregated results for a answers to a question using the Group By (Simplyfied Tables and Queries listed below). Quick run down on how things work. A Client can have many Questionnaires, a Questionnaire can have many Sections, a Section can have many questions. Each Question can have many pre-defined options. From the Questionnaire the User selected on of the pre-defined choices this is then saved into the Table UserQuestionnaireResponse. To get the data I can look at 2 tables UserQuestionnaireResponse and AboutYouQuestionsOptions. My test question asks: What fruit do you like? - If I do a query (QUERY 1 BELOW) to get all the users I get the result: If I do a query (QUERY 2 BELOW) by selecting one or more users, I get just the ones they selected How can I get the all aggregated values even when selecting one or more users? QUERY 1 QUERY 2
(comments are locked)
|
|
My first guess when looking at the code is in the WHERE clause. You have a set of ANDs and one OR. The way that they will be evaluated is probably not what you are wanting. You should put parentheses around the UserIds: Otherwise the WHERE clause is evaluating like this:
I guess you are wanting to compare the optiontype and the questionid against all users you chose, right? I would also take a look to see if the INNER JOIN on ---- EDIT - After a little more info, maybe this is what you are looking for: I took your explanation to mean, you want to always see all options and then the counts per option for the selected users. That is what the following code does. Hi WilliamD Iam aware of the parentheses (I just copied and pasted it), still doesn't work when using the correct condition even when selecting Just UserID 2 The OptionType is not required for this query, this gives me scope to add more than one list of options to a question. This would be set to either 0 or 1. I would use the QuestionType to determine this. I have trying all the filtering and Join types using VIEWS. just not able to get the aggregated table if I include the UserQuestionnaire Table. I jsut get the ones that have been selected by each users. This would be okay with 1000's of records the chance each option would have been choosen. Regards Kevin
Aug 10 '11 at 04:32 AM
TheBat
@TheBat - sorry for not being clear on that. The
Aug 10 '11 at 06:04 AM
WilliamD
Hi WilliamD I have tried LEFT, RIGHT, INNER's, OUTER's and even made up some. Still no luck. last example SELECT TOP 100 PERCENT dbo.AboutYouQuestionsOptions_en.OptionName, dbo.AboutYouQuestionsOptions_en.OptionValue, COUNT(dbo.UserQuestionnaireResponse.ResponseInt) AS [Total Responses] FROM dbo.AboutYouQuestionsOptions_en LEFT JOIN dbo.UserQuestionnaireResponse ON dbo.AboutYouQuestionsOptions_en.OptionValue = dbo.UserQuestionnaireResponse.ResponseInt RIGHT JOIN dbo.UsersQuestionnaire ON dbo.UserQuestionnaireResponse.UserQuestionnaireID = dbo.UsersQuestionnaire.ID WHERE ((dbo.AboutYouQuestionsOptions_en.QuestionID = 1) AND (dbo.UsersQuestionnaire.UserID >= 1)) GROUP BY dbo.AboutYouQuestionsOptions_en.OptionName, dbo.AboutYouQuestionsOptions_en.OptionValue ORDER BY dbo.AboutYouQuestionsOptions_en.OptionValue
Aug 10 '11 at 08:26 AM
TheBat
Hi William I really do appreciate the time you have given me. I am using SQL 2000 and could not get your example to work. so I have clean out all my tables and inserted your data. I did have a issue with the UserQuestionnaireResponse table would not let me delete 2 records even though I had drop all constraints. In the end I drop the table and re-created it. Anyway I have finally come to run your script which and get the following error. I have remove the @ as the query now looks at the data in the tables SELECT AYQU.OptionName, counts.Value FROM AboutYouQuestionsOptions_en AYQU OUTER APPLY (SELECT COUNT(UQR.ResponseInt) Value FROM UserQuestionnaireResponse UQR LEFT JOIN UsersQuestionnaire UQ ON UQ.ID = UQR.UserQuestionnaireID WHERE AYQU.QuestionID = UQR.QuestionID AND UQR.ResponseInt = AYQU.OptionValue AND UserId IN (1,2)) counts Server: Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'OUTER'. Server: Msg 170, Level 15, State 1, Line 9 Line 9: Incorrect syntax near 'counts'.
Aug 11 '11 at 06:19 AM
TheBat
(comments are locked)
|

