Hi @Brian ,
Welcome to the microsoft TSQL Q&A forum!
Your question is related to tsql, so my colleague helped you add the tsql tag so that you can get more professional help.
Please check:
declare @t as table (r1 varchar(20), r2 varchar(20), r3 varchar(20), col varchar(2))
insert into @t select 'Strongly Agree', 'Agree', 'Strongly Agree', 'r2'
insert into @t select 'Strongly Disagree', 'Agree', 'Strongly Agree', 'r2'
insert into @t select 'Agree', 'Agree', 'Strongly Agree', 'r3'
insert into @t select 'Disagree', 'Agree', 'Strongly Agree', 'r3'
insert into @t select 'Strongly Agree', 'Agree', 'Agree', 'r3'
;with cte as(
select *,case when col='r2' then r2 else r3 end [r2-r3]
from @t)
select distinct r1+ ' - '+cast(count(r1) over(partition by r1) as varchar(10))
+' (' +cast(count(r1) over(partition by r1)*100/count(r1)
over(order by (select 1)) as varchar(10))+ '%'+')' as r1
from cte
union all
select 'r2 / r3*'
union all
select distinct [r2-r3]+ ' - '+cast(count([r2-r3]) over(partition by [r2-r3]) as varchar(10))
+' (' +cast(count([r2-r3]) over(partition by [r2-r3])*100/count([r2-r3])
over(order by (select 1)) as varchar(10))+ '%'+')' [r2/r3]
from cte
Output:
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards,
Echo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.