查找NoOfComments的百分比

时间:2012-07-04 09:34:59

标签: sql-server-2008 sql-server-2008-r2

Id  Answer  NoOfComments
18      1        2
19      2        0
20      3        0
21      4        0
22      5        1

上面给出的数据是从以下StoredProcedure获得的输出。

 ALTER PROCEDURE [dbo].[BlogAnswerByQuestionId]
(
    @QuestionId int
)
AS
BEGIN
    SELECT [HRM_BlogAnswer].[Id] as Id
          ,[HRM_BlogAnswer].[Answer]
          ,(SELECT COUNT(*) FROM HRM_BlogVote  WHERE HRM_BlogVote.AnswerId  =[HRM_BlogAnswer] .[Id]) AS NoOfComments

        FROM [HRM_BlogAnswer] 
    WHERE [HRM_BlogAnswer].[QuestionId] = @QuestionId


END

现在我需要根据NoOfComments字段中的值找到每个答案的百分比值。我可以提供更多信息。 表HRM_blogquestion中的1.datas如下。

Id  Question    CreatedDate          CreatedBy
8   tttt    2012-07-03 17:36:47.513        1

表HRM_Bloganswer中的2.datas如下

Id  QuestionId  Answer
18  8             1
19  8             2
20  8             3
21  8             4
22  8             5

表HRM_Blogvote中的3.datas如下

Id  QuestionId  AnswerId    EmployeeId
19  8                18        1
23  8                22        24
24  8                18        25

从这些表数据我写上面的SP现在我需要找到投票答案的百分比

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您可以将现有查询转换为common table expression,而不是创建此表@Results。最终查询是您想要的:

declare @Results table (
    Id int not null,
    Answer int not null,
    NoOfComments int not null
)
insert into @Results (Id,Answer,NoOfComments) values
(18,      1,        2),
(19,      2,        0),
(20,      3,        0),
(21,      4,        0),
(22,      5,        1)

select *,((NoOfComments * 100.0) / SUM(NoOfComments) OVER (PARTITION BY (1))) as Pcnt
from @Results

结果:

Id          Answer      NoOfComments Pcnt
----------- ----------- ------------ ---------------------------------------
18          1           2            66.666666666666
19          2           0            0.000000000000
20          3           0            0.000000000000
21          4           0            0.000000000000
22          5           1            33.333333333333

所以,你的过程将是:

 ALTER PROCEDURE [dbo].[BlogAnswerByQuestionId]
(
    @QuestionId int
)
AS
BEGIN
    ;With Results as (
    SELECT [HRM_BlogAnswer].[Id] as Id
          ,[HRM_BlogAnswer].[Answer]
          ,(SELECT COUNT(*) FROM HRM_BlogVote  WHERE HRM_BlogVote.AnswerId  =[HRM_BlogAnswer] .[Id]) AS NoOfComments

        FROM [HRM_BlogAnswer] 
    WHERE [HRM_BlogAnswer].[QuestionId] = @QuestionId
    )
    select *,((NoOfComments * 100.0) / SUM(NoOfComments) OVER (PARTITION BY (1))) as Pcnt
    from Results

END

答案 1 :(得分:0)

ALTER PROCEDURE [dbo].[BlogAnswerByQuestionId] 
(     
@QuestionId int 
) 
AS 
BEGIN    
WITH Results 
AS 
    (     
        SELECT [HRM_BlogAnswer].[Id] as Id           
              ,[HRM_BlogAnswer].[Answer]           
              ,(SELECT COUNT(*) FROM [HRM_BlogVote]  WHERE [HRM_BlogVote].[AnswerId]  =[HRM_BlogAnswer].[Id]) AS NoOfComments          
          FROM [HRM_BlogAnswer]      
        WHERE [HRM_BlogAnswer].[QuestionId] = @QuestionId     
    )     
        SELECT *,
        case when NoOfComments>0 then  ((NoOfComments * 100.0) / SUM(NoOfComments) OVER (PARTITION BY (1))) else 0 end as Percentage    
            FROM Results  
END