Generate DAY, WEEK, MONTH and YEAR wise Report using SQL Query


--Here are the table Scripts

CREATE TABLE [dbo].[AttendanceMaster](
[attID] [int] IDENTITY(1,1) NOT NULL,
[empId] [int] NULL,
[attmonth] [int] NULL,
[attYear] [int] NULL,
[attStatus] [bit] NULL,
[remarks] [varchar](200) NULL,
[attdate] [datetime] NULL,
[loggedInDate] [datetime] NULL
) ON [PRIMARY]

--Run the Scripts to create table and run the below queries

--LastDay

SELECT empId, attmonth, attYear, attdate remarks FROM AttendanceMaster

WHERE Convert(varchar(12),attdate,101) = Convert(varchar(12),GETDATE()-1,101)

--Last 7 Day's

SELECT empId, attmonth, attYear, attdate remarks FROM AttendanceMaster

WHERE Convert(varchar(12),attdate,101) BETWEEN Convert(varchar(12),GETDATE()-7,101) AND Convert(varchar(12),GETDATE(),101)

--Last Month

SELECT empId, attmonth, attYear, attdate remarks, DATEPART(MM,getdate())-1 FROM AttendanceMaster

WHERE DATEPART(MM,attdate) = DATEPART(MM,getdate())-1


--This Month

SELECT empId, attmonth, attYear, attdate remarks FROM AttendanceMaster

WHERE DATEPART(MM,attdate) = DATEPART(MM,getdate())

--This Year

SELECT empId, attmonth, attYear, attdate remarks FROM AttendanceMaster

WHERE DATEPART(YYYY,attdate) = DATEPART(YYYY,getdate())-1

--Last Year

SELECT empId, attmonth, attYear, attdate remarks FROM AttendanceMaster

WHERE DATEPART(YYYY,attdate) = DATEPART(YYYY,getdate())-1

Comments