我有一个ServiceRating表,如下所示
基本上,上表记录了不同客户在5个不同问题上给特定供应商的费率。费率为5。
我需要显示上面的总结输出,如下所示
我使用以下查询获得此结果。
DECLARE @tempTable TABLE
(
Question varchar(200)
,[Excelent] int
,[Very Good] int
,[Good] int
,[Average] int
,[Poor] int
)
Insert INTO @tempTable
SELECT 'Q1'
,(SELECT COUNT(Q1) FROM ServiceRating Where Q1 = 5 )
,(SELECT COUNT(Q1) FROM ServiceRating Where Q1 = 4 )
,(SELECT COUNT(Q1) FROM ServiceRating Where Q1 = 3 )
,(SELECT COUNT(Q1) FROM ServiceRating Where Q1 = 2 )
,(SELECT COUNT(Q1) FROM ServiceRating Where Q1 = 1 )
Insert INTO @tempTable
SELECT 'Q2'
,(SELECT COUNT(Q2) FROM ServiceRating Where Q2 = 5 )
,(SELECT COUNT(Q2) FROM ServiceRating Where Q2 = 4 )
,(SELECT COUNT(Q2) FROM ServiceRating Where Q2 = 3 )
,(SELECT COUNT(Q2) FROM ServiceRating Where Q2 = 2 )
,(SELECT COUNT(Q2) FROM ServiceRating Where Q2 = 1 )
Insert INTO @tempTable
SELECT 'Q3'
,(SELECT COUNT(Q3) FROM ServiceRating Where Q3 = 5 )
,(SELECT COUNT(Q3) FROM ServiceRating Where Q3 = 4 )
,(SELECT COUNT(Q3) FROM ServiceRating Where Q3 = 3 )
,(SELECT COUNT(Q3) FROM ServiceRating Where Q3 = 2 )
,(SELECT COUNT(Q3) FROM ServiceRating Where Q3 = 1 )
Insert INTO @tempTable
SELECT 'Q4'
,(SELECT COUNT(Q4) FROM ServiceRating Where Q4 = 5 )
,(SELECT COUNT(Q4) FROM ServiceRating Where Q4 = 4 )
,(SELECT COUNT(Q4) FROM ServiceRating Where Q4 = 3 )
,(SELECT COUNT(Q4) FROM ServiceRating Where Q4 = 2 )
,(SELECT COUNT(Q4) FROM ServiceRating Where Q4 = 1 )
Insert INTO @tempTable
SELECT 'Q5'
,(SELECT COUNT(Q5) FROM ServiceRating Where Q5 = 5 )
,(SELECT COUNT(Q5) FROM ServiceRating Where Q5 = 4 )
,(SELECT COUNT(Q5) FROM ServiceRating Where Q5 = 3 )
,(SELECT COUNT(Q5) FROM ServiceRating Where Q5 = 2 )
,(SELECT COUNT(Q5) FROM ServiceRating Where Q5 = 1 )
SELECT * FROM @tempTable
问题:有没有其他方式(使用支点)或比上述更好的方式来获得理想的结果
答案 0 :(得分:1)
您可能需要通过问题进行解析,然后再按费率转动,如下所示:
select rates, [5] as 'Excellent' ,[4] as 'Very Good',
[3] as 'Good',[2] as 'Average' ,[1] as 'Poor'
From(
SELECT *
FROM
(SELECT rateTo, q1,q2,q3,q4,q5
FROM ServiceRating) p
UNPIVOT
(rate FOR rates IN
(q1,q2,q3,q4,q5)
) as unpvt
) x
pivot
(
count(rate)
for rate in
([5],[4],[3],[2],[1])
) as pvt