USE [BiometricDB]
GO
/****** Object: StoredProcedure [dbo].[sp_GetTop3VolunteerReport] Script Date: 12/5/2022 9:45:34 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetTop3VolunteerReport]
--@inVolunteerID int,
--@inType varchar(100),
--@inShift varchar(100), current_date
@inFromDateTime datetime = '20220301', --yyyymmdd
@inToDateTime datetime = '20220331' --yyyymmdd
--@inPoints int
AS
BEGIN
select top(3)
ROW_NUMBER() over (order by temp.TotalPoints desc) as "Rank",
vol.profilephoto as Photo,
pos.positionname as Position,
vol.reportdisplayname as "Volunteer Name",
vol.GroupName as "Group",
temp.MPoints as "Morning Points",
temp.EPoints as "Evening Points",
temp.ExPoints as "Extra Points",
temp.TotalPoints as "Total Points",
temp.MDays as "Morning Days",
temp.EDays as "Evening Days",
temp.ExDays as "Extra Days",
temp.TotalDays as "Total Days",
temp.TotalDuration as "Total Duration"
from
(select volunteer.volunteerpositionid as Position,
volunteer.volunteerid as VolunteerID,
SUM(CASE when attendance.shift = 'Morning' then attendance.points else 0 end) as MPoints,
SUM(CASE when attendance.shift = 'Evening' then attendance.points else 0 end) as EPoints,
SUM(CASE when attendance.shift = 'Special' then attendance.points else 0 end) as ExPoints,
SUM(isnull(attendance.points, 0)) as TotalPoints,
COUNT(CASE when attendance.shift = 'Morning' then attendance.points end) as MDays,
COUNT(CASE when attendance.shift = 'Evening' then attendance.points end) as EDays,
COUNT(CASE when attendance.shift = 'Special' then attendance.points end) as ExDays,
COUNT(attendance.points) as TotalDays,
--SUM(isnull(CAST(duration/60.0 as DECIMAL(16, 2)), 0)) as TotalDuration
CAST( ( SUM( isnull( duration, 0 ) )/60 ) + ( ( SUM( isnull( duration, 0 ) )%60 )/100.0 ) as DECIMAL(16, 2) ) as TotalDuration
from tblvolunteer volunteer
left outer join tblattendancelog attendance
on attendance.volunteerid = volunteer.volunteerid
and attendance.type = 'Present'
--where (tblattendancelog.volunteerid = @inVolunteerID or @inVolunteerID = 0)
--and (type = @inType or @inType = '< All >')
--and (shift = @inShift or @inShift = '< All >')
and (attendance.dutydatetime between @inFromDateTime and @inToDateTime)
--and (points = @inPoints or @inPoints = -1)
where volunteer.isresign = 0
--and volunteer.volunteerpositionid != 6
group by volunteer.volunteerpositionid, volunteer.volunteerid
) temp
inner join tblvolunteerposition pos
on temp.Position = pos.volunteerpositionid
inner join tblvolunteer vol
on temp.VolunteerID = vol.volunteerid;
END
Set Defualt Value Of InfromDate And ToDate to use the first And last day of current month