Quantcast
Channel: SQL Server Reporting Services
Viewing all articles
Browse latest Browse all 1418

Can you please help me modify this query to group results?

$
0
0

Helo Experts,

The query below helps us display results of survey.

The result is filter by surveyId (where surveyId = someId) and it works great.

But now, owners of the app would like us to modiy the code group results by location.

For instance, the current code produces results similar to this:

Total Questions:________              Total Correct:_______________              %Correct:_________________

Here is the cdoe that does the above:

WITH Questions AS
          ( SELECT SQ.QuestionID,
            CorrectChoices = COUNT(NULLIF(SC.IsCorrect, 0)),
            ChoicesGiven = COUNT(SA.ChoiceID),
            CorrectChoicesGiven = COUNT(CASE WHEN SA.ChoiceID IS NOT NULL AND SC.IsCorrect = 1 THEN 1 END),
            ExtraChoicesGiven = CASE WHEN COUNT(SA.ChoiceID) > COUNT(NULLIF(SC.IsCorrect, 0)) THEN COUNT(SA.ChoiceID) - COUNT(NULLIF(SC.IsCorrect, 0)) ELSE 0 END
            FROM SurveyQuestions SQ
            INNER JOIN SurveyChoices SC
                ON SQ.QuestionId = SC.QuestionID
            LEFT JOIN SurveyAnswers SA
                ON SA.QuestionId = SC.QuestionID
                AND SA.ChoiceID = SC.ChoiceID
    WHERE   SQ.SurveyID = 8
    GROUP BY SQ.QuestionID
), QuestionScores AS
(   SELECT  QuestionID,
            Score = CASE WHEN CorrectChoicesGiven - ExtraChoicesGiven < 0 THEN 0
                        ELSE CAST(CorrectChoicesGiven - ExtraChoicesGiven AS FLOAT) / CorrectChoices
                    END,
            Score2 = ISNULL(CAST(CorrectChoicesGiven AS FLOAT) / NULLIF(CASE WHEN ChoicesGiven > CorrectChoices THEN ChoicesGiven ELSE CorrectChoices END, 0), 0)
    FROM    Questions
)
SELECT  TotalQuestions = COUNT(*),
        TotalCorrect = SUM(Score),
        PercentCorrect = CAST(100.0 * SUM(Score) / COUNT(*) AS DECIMAL(5, 2)),
        TotalCorrect2 = SUM(Score2),
        PercentCorrect2 = CAST(100.0 * SUM(Score2) / COUNT(*) AS DECIMAL(5, 2))
FROM    QuestionScores;

Now, they want the code modify to produce results similar to this:

Location:___Location Name goes here_

 Survey 1 Results:

         John Doe           

                  Total Questions:________________   Total Correct:___________________  %Correct:_______________

         Jane Doe           

                  Total Questions:________________   Total Correct:___________________  %Correct:_______________

 

         Peter Pan

                  Total Questions:________________   Total Correct:___________________  %Correct:_______________

              

 Survey 2 Results:

         John Doe           

                  Total Questions:________________   Total Correct:___________________  %Correct:_______________

         Jane Doe           

                  Total Questions:________________   Total Correct:___________________  %Correct:_______________

 

         Peter Pan

                  Total Questions:________________   Total Correct:___________________  %Correct:_______________

There are a total of 8 surveys.

Once all surveys and survey takers are displayed for a particular location, the entire process is repeated for next location.

Essentially, each location displays results of survey participants and their survey results until locations are represented.

A few more helpful information.

Location belongs to a table called Locations.

There is a relationship between Locations table and SurveyAnswers table based on UserName.

I am not sure if I have provided enough information.

I will be glad to provide more if needed.

Thank you.


Viewing all articles
Browse latest Browse all 1418

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>