Para encontrar el porcentaje de NoOfComments

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

The datas given above is the output obtained from following 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

Now I need to find the percentage value of each answer according to the value in the field NoOfComments. I can give some more information. 1.datas in table HRM_blogquestion as follows.

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

2.datas in table HRM_Bloganswer as follows

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

3.datas in table HRM_Blogvote as follows

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

From these table datas i write the above SP And now i need to find percentage of voted answers

Por favor ayúdame a solucionar esto.

preguntado el 04 de julio de 12 a las 10:07

Is the result (from your sample) 66%,0%,0%,0%,33% (for each of the 5 rows)? If not, what percentage is expected to be calculated? Will there always only be 5 rows? If not, is it always across the entire table, or is there some further column that identifies related rows? -

Result is as same as u give. but it is not always 5 rows -

2 Respuestas

Rather than creating this table @Results, you could turn your existing query into a expresión de tabla común, and query that instead. The final query is the one you want:

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

Resultado:

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

So, your proc would be:

 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

Respondido 04 Jul 12, 11:07

here NoOfComments is not a field in table.It is shown only in the output of the above SP. ,(SELECT COUNT(*) FROM HRM_BlogVote WHERE HRM_BlogVote.AnswerId =[HRM_BlogAnswer] .[Id]) AS NoOfComments is the query to show the field NoOfComments in output - Ramdas

@Ramdas - that's what I was referring to in my first paragraph, where I said you'd convert your existing query to a CTE. I've updated my answer (at the end) with what I think your stored proc would look like. - damien_el_incrédulo

Dear Damien, Thanks a lot for your help. Now I meet to my requirement. Thank you verymuch. - Ramdas

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 

Respondido 09 Jul 12, 13:07

In the above answer a problem may occur during NoOfComments is 0. That is dividing by 0 is not possible. I think this answer will solve that problem. - Ramdas

Can I use this example @Damien - Ramdas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.