他のクエリと同様に、ピボット クエリの結果をフォーマットできます。そのため、パーセンテージをそれぞれのソースに連結できます。 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