Thursday, 16 May 2013

how to get records for last week and this week?

Hi here today i'll show you how to get records for current week and past week to shown up. here for service
fastness i done this in sql by writing stored procedure as follows:  my SP name is "GetUserLogsByweek"

USE [DB]
GO
/****** Object:  StoredProcedure [dbo].[GetUserLogsByweek] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetLogsByweek](@Duration nvarchar(50))

AS
BEGIN
DECLARE @LastweekStartDate DATETIME, @ThisweekStartDate DATETIME, @NextweekStartDate DATETIME
SET @LastweekStartDate = DATEADD(week, DATEDIFF(week,0,GETDATE())-1,-1)
SET @ThisweekStartDate = DATEADD(week, DATEDIFF(week,0,GETDATE()),-1)
SET @NextweekStartDate =DATEADD(week, DATEDIFF(week,0,GETDATE())+1,-1)

--if(@Duration='Lastweek')
begin

select count([ID])  as 'WeekCount' from tbllogs where [DateCreated] between @LastweekStartDate and @ThisweekStartDate and [ID] = 2 group by [UserID] 

end

--if(@Duration='Thisweek')
begin

select count([ID]) as 'WeekCount' from tbllogs  here [DateCreated] between @ThisweekStartDate and @NextweekStartDate and [ID] = 2 group by [UserID] 
end

END


in this above procedure i'll get the count of the records that the last week that by passing parameter to SP for "@Duration " like  "Lastweek" /  "Thisweek" then it will gives the appropriate records.

No comments:

Post a Comment