Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table named attendance with column named Pre(Bit type) for marking both absent and present.true for present and false for absent i am calculating total classes and attended classes using this column.
now the issue is i cannot think of a way to calculate percentage based on the values in the virtual columns i.e total classes and Attended.
is there any way i can use these column values to calculate percentage in third virtual column named Percentage?

What I have tried:

select distinct s.S_ID,s.Name
 ,(select  count(distinct A_Date) from Attendance
 where  a.C_ID=C_ID ) as Total_Classes,
 (select count(Pre) from Attendance where a.C_ID = C_ID
 and a.S_ID = S_ID and Pre = 'True') as Attended

/* (select count((Pre))/count(distinct A_Date) from Attendance where a.S_ID=S_ID and a.C_ID=C_ID) as Percentage*/
 from Course c full  join StudentCourse sc on c.C_ID = sc.C_ID
 full  join Student s on s.S_ID = sc.S_ID
 full   join Attendance a on a.S_ID = s.S_ID and a.C_ID = c.C_ID where c.C_ID =6
Posted
Updated 21-Jun-19 2:58am

1 solution

;with cte as (
select distinct s.S_ID,s.Name
  ,(select  cast(count(distinct A_Date)as float)from Attendance 
  where  a.C_ID=C_ID ) as Total_Classes,
  (select count(Pre) from Attendance where a.C_ID = C_ID 
  and a.S_ID = S_ID and Pre = 'True') as Attended 
  from Course c left outer join StudentCourse sc on c.C_ID = sc.C_ID 
  left outer join Student s on s.S_ID = sc.S_ID 
  left outer  join Attendance a on a.S_ID = s.S_ID and a.C_ID = c.C_ID where c.C_ID=6)
  select *,
  cast(Attended*100/nullif(Total_Classes,0) as decimal(10,2))  Percentage 
  from cte 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900