SQL
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 = '')
--and (shift = @inShift or @inShift = '')
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;
ENDSet Defualt Value Of InfromDate And ToDate to use the first And last day of current month

Amit MohantyRank #112 · 20K reputationPosted Dec 5, 2022, 11:22 PM EST
Use this
SELECT [StartOfMonth] = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0), [EndOfMonth] = EOMONTH(GETDATE())Guest UserRank #1,881 · 787 reputationPosted Dec 5, 2022, 11:24 PM EST
can u add me on my code
Amit MohantyRank #112 · 20K reputationPosted Dec 5, 2022, 11:29 PM EST
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", [StartOfMonth] = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0), [EndOfMonth] = EOMONTH(GETDATE()) 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 = '') --and (shift = @inShift or @inShift = '') 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; ENDGuest UserRank #1,881 · 787 reputationPosted Dec 5, 2022, 11:33 PM EST
thank You
Bhai code direct data show kar ga month k
here no need parameter
Amit MohantyRank #112 · 20K reputationPosted Dec 5, 2022, 11:36 PM EST
Yes it will show current month first and last date. If you want first and last date of a particular date then replace GETDATE() with your date parameter.
Rajesh GamiRank #30 · 41.5K reputationPosted Dec 6, 2022, 1:13 AM EST
Use this
****************** Get the First and Last Day of a Month ******************
However, we want the First and Last day (in words). So, simply embed the two above queries inside the DATENAME() function. Here it is.