Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:42:08+00:00 2026-06-07T22:42:08+00:00

This is a follow up question from here . I have some output and

  • 0

This is a follow up question from here.
I have some output and was able to add a total column but now I need to use the column to add percentage like this:

| LESSONID     RESPONSE COUNT-> | 0       | 1       | 2       | 3       | 4       | N |
---------------------------------------------------------------------------------------                         
|lesson1                        | 1 (20%) | 1 (20%) | 1 (20%) | 1 (20%) | 1 (20%) | 5 |
|lesson2                        | 1 (20%) | 1 (20%) | 1 (20%) | 2 (40%) | 0       | 5 |
|lesson3                        | 1 (20%) | 1 (20%) | 0       | 3 (60%) | 0       | 5 |
|lesson4                        | 0       | 1 (20%) | 4 (80%) | 0       | 0       | 5 |
|lesson5                        | 0       | 5 (100%)| 0       | 0       | 0       | 5 |

I have found some help here but can’t get it all to come together.

Here is what I have so far

*Also I am on SQL Server 2005 which is not represented in SQLFiddle

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:42:10+00:00Added an answer on June 7, 2026 at 10:42 pm

    You can format the result of your pivot query as you would do with any other. So, you might concatenate percentage to its respective source. Here is a SQL FIDDLE.

    SELECT RC.lessonid AS 'lessonid     response count->'
      , convert (varchar(20), isnull([0], 0))
      + isnull (' ('
    -- As both numbers are integers don't forget to cast one of them into double
    -- If you dislike * 100 format or want more precise result.
      + convert (varchar(20), [0] * 100 / RCN.N)
      + '%)', '') as [0]
      , convert (varchar(20), isnull([1], 0))
      + isnull (' ('
      + convert (varchar(20), [1] * 100 / RCN.N)
      + '%)', '') as [1]
      , convert (varchar(20), isnull([2], 0))
      + isnull (' ('
      + convert (varchar(20), [2] * 100 / RCN.N)
      + '%)', '') as [2]
      , convert (varchar(20), isnull([3], 0))
      + isnull (' ('
      + convert (varchar(20), [3] * 100 / RCN.N)
      + '%)', '') as [3]
      , convert (varchar(20), isnull([4], 0))
      + isnull (' ('
      + convert (varchar(20), [4] * 100 / RCN.N)
      + '%)', '') as [4]
      ,RCN.N
    FROM (
        SELECT lessonid
            ,response
            ,count(response) AS respcnt
        FROM tblRChoices
        GROUP BY lessonid
            ,response
        ) TableResponseCount
    PIVOT(SUM(respcnt) FOR response IN (
                [0]
                ,[1]
                ,[2]
                ,[3]
                ,[4]
                )) RC
    JOIN (SELECT lessonid, count(lessonid) as N FROM tblRChoices GROUP BY lessonid) RCN 
    ON RC.lessonid = RCN.lessonid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This Question arises from a Q&A here I have some doubts that i think
This is a follow up question from my previous one found here I need
This is a follow-on question from the one I asked here . Can constraints
This is a follow up from my previous question I have this code basically
Question follows on from Fluent NHibernate + multiple databases (no need to follow this
All, this is a follow up from a previous question here: C# formatting external
There are some other variations of this question here at SO, but please read
This is a follow up of another question here on SO . I have
This is a follow on question from my previously answered question here: Reading characters
This is a follow on question to an earlier one. I have learned some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.