简体   繁体   中英

SQL Server 2005 percentage in parenthesis in pivot table

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

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM