SQL table joins query -
I am trying to calculate responses to a questionnaire system. I want to show results in a table (questions, options, number of reactions) I wrote a question which works just fine but it does not display all the options and if there is no response for them.
My query
SELECT R.QuestionID, Q.QuestionName, A.OptionName, (R.OptionID) as answer, as A.OptionID COUNT response Include R Inner Question Q.QuestionID = R.QuestionID on the Q as an option R.OptionID = A option included in the OptionID WHERE (R.QuestionnaireID = 122) Group R R QUESTONID, Q Question Questionnaire, A. Ocean, R. Question ID, AOSPIDID
database structure
Table Definition
Create Table [DBO]. [Questionnaire] ([Push (Questionnaire name) NVARCHAR (100) faucet, primary key class ([questionnaire] ASC), create table [dbo]. [Question] ([QuestionID] INT Identification (1, 1) No, zero, (1, 1) No NULL, [QuestionnaireID] INT No NULL, [QuestionName] NVARCHAR (250) No NULL, Primary Key Cluster ([QuestionID] ASC), [FK_ Q & A] External Key [[Questionerate]] Reference [DBO ]. [Questionnaire] ([questionnaire])); CREATE TABLE [dbo]. [Option] INT detection (1, 1) No NULL, [QuestionID] zero not INT, [OptionName] NVARCHAR (150) No NULL, Primary key cluster ([OptionID] ASC), [FK_Option_Question] Foreign key [ (Question)] Reference [DBO]. [Question] ([Question ID]); Create table [dbo]. [ResponseID] INT Identity (1, 1) No NULL, [QuestionnaireID] INT No NULL, [QuestionID] INT No NULL, [VAL] NVARCHAR (150) No NULL, [OptionID] INT Zero, Primary Key Cluster ([ResponseID] ASC), [FK_Response_Option] Foreign Key ([OptionID]) Reference [dbo]. [Option] ([OptionID]), Obligate [FK_Response_Question] Foreign Key ([QuestionID]) Reference [DBO]. [ Question] ([question ID]), Constantant [FK_Response_QuestonNair] Foreign Key ([questionnaire]] reference [DBO]. [Questionnaire] ([questionnaire]));
Current data:
Enter the questionnaire values ('asp.NET questionnaire'); Enter the questionnaire values ('Test Questionnaire'); Enter question values (2, 'rate our services'); Insert question values (2, 'on the scale of 1 to 5, how much sleep do you have?'); Insert question values (2, 'how are you today'); Insert in [Option] (1, 'Good'); Insert in [Option] (1, 'Bad'); Insert in [Option] (1, 'Medium'); Insert [Option] Price (2, '1'); Insert [option] value (2, '2'); Insert [option] value (2, '3'); Insert [Option] Value (2, '4'); Insert [Option] Value (2, '5'); [Option] Enter values (3, 'OK'); [Option] Insert value (3, 'Great'); [Option] Insert values (3, 'Not bad'); [Option] Insert value (3, 'bad'); Insert the response values (2, 1, 'good', 1); Insert the response values (2, 1, 'good', 1); Insert the response values (2, 1, 'bad', 2); Insert the response values (2, 1, 'good', 1); Insert the response values (2, 2, '1', 4); Insert the response values (2, 2, '3', 3); Insert the response values (2, 2, '4', 5); Insert the response values (2, 2, '5', 8); The "Output"> SQL Bedle
P> Edit I updated the reply based on your SQL Belal. It works and gives you your desired output.
SELECT Q.QuestionName AS question, A.OptionName AS [Option], Answer as answer to COUNT (R.OptionID) Q Inner Join [Options] AKAUVVIIID = Q.questionID join the left as Q.QuestionID = R.QuestionID and R.OptionId = A.Optionid on the R form of question (Q.QuestionnaireID = 2) by Group Q.QuestionID, Q.QuestionName, Q.QuestionName .OptionName command, A.OptionName
Comments
Post a Comment