sql server - Summary of Service rating in SQL -


i have servicerating table given below

enter image description here
basically, above table record rate given specific vendor differnet customer across 5 different questions. rate given out of 5. need show summarize output of above given below

enter image description here

i have achive result using below query.

declare @temptable table     (          question varchar(200)          ,[excelent] int         ,[very good] int         ,[good] int         ,[average] int         ,[poor] int      ) 

insert @temptable select 'q1'     ,(select count(q1) servicerating q1 = 5 )     ,(select count(q1) servicerating q1 = 4 )     ,(select count(q1) servicerating q1 = 3 )     ,(select count(q1) servicerating q1 = 2 )     ,(select count(q1) servicerating q1 = 1 )  insert @temptable select 'q2'     ,(select count(q2) servicerating q2 = 5 )     ,(select count(q2) servicerating q2 = 4 )     ,(select count(q2) servicerating q2 = 3 )     ,(select count(q2) servicerating q2 = 2 )     ,(select count(q2) servicerating q2 = 1 )  insert @temptable select 'q3'     ,(select count(q3) servicerating q3 = 5 )     ,(select count(q3) servicerating q3 = 4 )     ,(select count(q3) servicerating q3 = 3 )     ,(select count(q3) servicerating q3 = 2 )     ,(select count(q3) servicerating q3 = 1 )      insert @temptable select 'q4'     ,(select count(q4) servicerating q4 = 5 )     ,(select count(q4) servicerating q4 = 4 )     ,(select count(q4) servicerating q4 = 3 )     ,(select count(q4) servicerating q4 = 2 )     ,(select count(q4) servicerating q4 = 1 )                              insert @temptable select 'q5'     ,(select count(q5) servicerating q5 = 5 )     ,(select count(q5) servicerating q5 = 4 )     ,(select count(q5) servicerating q5 = 3 )     ,(select count(q5) servicerating q5 = 2 )     ,(select count(q5) servicerating q5 = 1 ) 

select * @temptable     

question : there alternative way(using pivot) or better above desire result

you may need unpivot question , pivot again rate, this:

select rates, [5] 'excellent' ,[4] 'very good',               [3] 'good',[2] 'average' ,[1] 'poor' from( select *     (select rateto, q1,q2,q3,q4,q5    servicerating) p unpivot    (rate rates in        (q1,q2,q3,q4,q5) ) unpvt ) x  pivot (  count(rate) rate in  ([5],[4],[3],[2],[1]) ) pvt 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -