Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 display monthly data into no of weeks

Author  Topic 

Mahesh_1986
Starting Member

2 Posts

Posted - 2012-02-27 : 07:29:00
Week start from friday to thursday
for Example
i have a table (Ecode,Name ,No_Error,Answered_Handled,Date,Tol_Question)
we need calcuate these values weekly basis for example FEB-2012

01-02-2012 to 02-02-2012 1st week
03-02-2012 to 09-02-2012 2nd week
10-02-2012 to 16-02-2012 3rd week
17-02-2012 to 23-02-2012 4th week
24-02-2012 to 29-02-2012 5th week

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-27 : 09:33:48
You will need to create a calendar table (virtual or base) that has the information on the weeks. For example, one way to create the calendar table is as follows - where I am specifying the start of the month and the day on which the week starts using variables:

DECLARE @dt DATETIME; SET @dt = '20120201';
DECLARE @weekstart INT; SET @weekstart = 3; -- Thursday.

;WITH weeks AS
(
SELECT
1 AS WeekNumber,
@dt AS StartDate,
CASE
WHEN @weekstart = DATEDIFF(dd,'19000101',@dt)%7 THEN @dt + 6
ELSE @dt + (7+@weekstart-DATEDIFF(dd,'19000101',@dt)%7)%7
END AS EndDate
UNION ALL
SELECT
WeekNumber+1,
EndDate+1,
CASE
WHEN EndDate + 7 >= DATEADD(MONTH,1,@dt) THEN DATEADD(dd,-1,DATEADD(MONTH,1,@dt))
ELSE EndDate + 7
END
FROM
weeks
WHERE
EndDate + 1 < DATEADD(MONTH,1,@dt)
)
SELECT * FROM weeks;
Once you have the calendar table, you can join your own tables to the calendar table and do aggregations etc. to suit your needs.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-27 : 19:27:17
here's one function i have writen based on same logic

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -