0

我正在使用以下过程来检索总数并presents使用数据透视查询....我absents想要然后SUMPresents[P]Absents[A]calculate percentage

ALTER PROCEDURE [dbo].[GetAttendence](@Course_Id varchar(30), @Semester varchar(10))

AS
   SELECT DISTINCT Enroll_Number, Course_Id, Semester, Isnull([P],0) 
                                           As Presents, Isnull([A],0) As Absents

    FROM (SELECT Enroll_Number, Course_Id, Semester, Flag from Attendence ) ps
    pivot(Count(Flag) for Flag in ([P],[A])) as pvt 

    WHERE Course_Id = @Course_Id and Semester = @Semester

这是Forula为了实现......

总和 = 出席 + 缺席

百分比 = ((礼物/总和) * 100)

4

1 回答 1

0

试试这个:

select Enroll_Number,
       Course_Id,
       Semester,
       P as Presents,
       A as Absents,
       P+A as SumPA,
       1.0*P/(P+A)*100 as Percentage
from
  (
    select Enroll_Number, Course_Id, Semester, Flag
    from Attendence
    where Course_Id = @Course_Id and 
          Semester = @Semester
  ) ps
pivot
  (
    count(Flag) for Flag in ([P],[A])
  ) as pvt 
于 2012-06-15T07:27:17.620 回答