计算调查表的响应百分比

时间:2016-06-08 19:04:13

标签: sql-server database

我有一张表格,用于存储调查数据。每项调查都有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%  

有人可以帮忙吗?

由于

3 个答案:

答案 0 :(得分: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

答案 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%

答案 2 :(得分:0)

所以,我使用临时表#terecrecordset而不是你的表。因此,请在使用代码时更换它。我使用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 QuestionId=@questionId)
  set @countofQuestion = (select count(*) from #temprecordset where QuestionId=@questionId)
  set @percentofQuestionsAnswered = @countofAnswer*100/@countofQuestion
  insert into #QuestionPercent values (@questionId,@percentofQuestionsAnswered)
  SET @questionId =@questionId + 1 
  END


  select * from #QuestionPercent