2016-06-08 13 views
-1

我有一個表格用於存儲調查數據。每個調查是如果5個問題。用戶對每個問題的回答可以是1或0,並且每個調查都有與之相關的日期。調查表的響應計算百分比

我需要提出一個查詢,以便我可以得到每個問題回答'1'的人的年齡百分比。

我的示例數據:

RecId | AnswerdId | QuestionId | Answer | Date 
---------------------------------------------------- 
1   1   1   1  6/1/2016 
2   1   2   0  6/1 
3   1   3   1  6/1 
4   1   4   0  6/1 
5   1   5   1  6/1 
6   2   1   0  6/2 
7   2   2   0  6/2 
8   2   3   1  6/2 
9   2   4   1  6/2 
10   2   5   1  6/2 

我需要一個像

Question1 Question2 Question3 Question4 Question5 
    50%   0%  100%  50%  100% 

輸出誰能幫助?

感謝

+1

這是什麼部分你有麻煩具體? –

回答

0
Declare @Table table (RecId int,AnswerID int,QuestionID int,Answer int,Date Date) 
Insert into @Table (RecId,AnswerID,QuestionId,Answer,Date) values 
(1,1,1, 1,'6/1/2016'), 
(2,1,2, 0,'6/1/2016'), 
(3,1,3, 1,'6/1/2016'), 
(4,1,4, 0,'6/1/2016'), 
(5,1,5, 1,'6/1/2016'), 
(6,2,1, 0,'6/2/2016'), 
(7,2,2, 0,'6/2/2016'), 
(8,2,3, 1,'6/2/2016'), 
(9,2,4, 1,'6/2/2016'), 
(10,2,5, 1,'6/2/2016') 

;with cteSum as (Select QuestionID,Pct = sum(Answer)/(count(*)+0.0) From @Table Group By QuestionID) 
Select Question1 = format(sum(case when QuestionID=1 then Pct else 0 end),'0%') 
     ,Question2 = format(sum(case when QuestionID=2 then Pct else 0 end),'0%') 
     ,Question3 = format(sum(case when QuestionID=3 then Pct else 0 end),'0%') 
     ,Question4 = format(sum(case when QuestionID=4 then Pct else 0 end),'0%') 
     ,Question5 = format(sum(case when QuestionID=5 then Pct else 0 end),'0%') 
From cteSum 

返回

Question1 Question2 Question3 Question4 Question5 
50%   0%   100%  50%   100% 
+0

我應該加上。問題3是100%,而不是顯示的50% –

+0

謝謝!這很好.. – Coder77

1

的 「絕招」 是使用CASE語句SUM函數內部,決定有哪些回答值應予以考慮。在下面的例子中,SELECT它是最內層的查詢,你可以計算百分比。

當你想在不同的顯示每一個值,你還可以使用@JohnCappelletti提出同樣的做法,並分別計算每列,也可以是結果集PIVOT。請注意,如果不使用動態SQL,則無法動態讀取答案值(表示爲[1][2]等),好嗎?

我更願意將所有結果作爲十進制返回,因爲表示層(而不是數據層)應該通過格式化該值來負責。

;WITH SurveyData AS 
(
    SELECT RecId, AnswerdId, QuestionId, Answer 
    FROM ( VALUES 
       (01, 1, 1, 1), 
       (02, 1, 2, 0), 
       (03, 1, 3, 1), 
       (04, 1, 4, 0), 
       (05, 1, 5, 1), 
       (06, 2, 1, 0), 
       (07, 2, 2, 0), 
       (08, 2, 3, 1), 
       (09, 2, 4, 1), 
       (10, 2, 5, 1) 
      ) AS Sample(RecId, AnswerdId, QuestionId, Answer) 
) 
SELECT ISNULL(SUM([1]), 0) [Question1], 
     ISNULL(SUM([2]), 0) [Question2], 
     ISNULL(SUM([3]), 0) [Question3], 
     ISNULL(SUM([4]), 0) [Question4], 
     ISNULL(SUM([5]), 0) [Question5] 
FROM (
     SELECT  QuestionId, 
        SUM(CASE WHEN Answer = 1 THEN 1.0 ELSE 0.0 END) Yes, 
        SUM(CASE WHEN Answer = 0 THEN 1.0 ELSE 0.0 END) No , 
        SUM(CASE WHEN Answer = 1 THEN 1.0 ELSE 0.0 END)/COUNT(Answer) Perc 
     FROM  SurveyData 
     GROUP BY QuestionId 
     )   AS SourceTable 
PIVOT 
     (   SUM(Perc) 
        FOR QuestionId IN ([1], [2], [3], [4], [5]) 
     )   AS PivotTable 
0

所以,我用臨時表#temprecordset而不是你的表。所以,請在使用您的代碼時將其替換。我使用了一個while循環來從最小的到最大的questionId循環,並使用臨時表格計算百分比。

確保在完成打印結果後刪除臨時表。希望這可以幫助。

declare @questionId Integer 
declare @maxQuestionId Integer 
declare @countofAnswer Integer 
declare @countofQuestion Integer 
declare @percentofQuestionsAnswered decimal 

set @questionId=1 
set @maxQuestionId = (select max(questionId) from #temprecordset) 

WHILE (@questionId <= @maxQuestionId) 
    BEGIN 
    set @countofAnswer = (select sum(answer) from #temprecordset where [email protected]) 
    set @countofQuestion = (select count(*) from #temprecordset where [email protected]) 
    set @percentofQuestionsAnswered = @countofAnswer*100/@countofQuestion 
    insert into #QuestionPercent values (@questionId,@percentofQuestionsAnswered) 
    SET @questionId [email protected] + 1 
    END 


    select * from #QuestionPercent 
+0

你應該避免在SQL中循環並開始考慮集合 –

+0

是的。我儘量避免循環,但我在這裏使用它的原因是爲了增加更多的問題。如果有100個問題,它仍然可以在select語句中稍作調整。 –

+0

不過,你應該避免循環;沒有必要計算_n_次聚合函數 –