2

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

Community
  • 1
  • 1
gooddadmike
  • 2,329
  • 4
  • 26
  • 48
  • 1
    You might to add `SQL` to list of tags when asking questions. I believe that this would double the view count thus increasing your chance of getting a decent answer. – Nikola Markovinović Jul 13 '12 at 10:06

2 Answers2

2

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
Nikola Markovinović
  • 18,963
  • 5
  • 46
  • 51
2

You can do this by casting your values to strings and then concatenating it into one string (SQL Fiddle with Demo of it in one field). Similar to this:

SELECT RC.lessonid AS 'lessonid     response count->'
 , cast(isnull([0], 0) as varchar(10)) 
   + isnull(' (' + cast([0]*100/RCN.N as varchar(10)) + '%)', '') as [0]

However, my question would be do you need this displayed in the same column as a string. Are you going to use this in a front-end application for anything? If so, then you might want to consider placing the percentage in a separate column so then you will still have the initial value as an int. (See SQL Fiddle with Demo) Doing it this way gives you more flexibility based on how you are going to use this data because then you would not have to strip the percentage out to then get the initial value.

SELECT RC.lessonid AS 'lessonid     response count->'
  , cast(isnull([0], 0) as varchar(10))  as [0]
  , isnull(' (' + cast([0]*100/RCN.N as varchar(10)) + '%)', '') as [%_0]
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • You are totally correct. The customer thinks they want it this way. I am going to provide it with just the count,just the percentage,count + percentage, and count with percentage in next column. Let them decide. – gooddadmike Jul 13 '12 at 16:36
  • Honestly I don't know how to reward the correct answer. You helped me last time. Both answers are correct. Nikola answered first. – gooddadmike Jul 13 '12 at 16:54
  • 1
    They answered first, I only answered to offer an alternative suggestion and perspective on the problem. – Taryn Jul 13 '12 at 16:56