![](/img/trans.png)
[英]How to find sum of multiple columns in a table in SQL Server 2005?
[英]How to find Sum Of Pivoting two columns in SQL Server 2005?
我使用下面的步驟檢索總presents
和absents
使用樞查詢....我想SUM
的Presents[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)
嘗試這個:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.