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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 group by week(according calender)

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 02:48:31
CREATE TABLE #Tempplaydate(
date1 datetime,
quantity int)
DECLARE @x INT
SET @x = 0
WHILE @x <= 70
BEGIN
INSERT INTO #Tempplaydate VALUES (getdate()+@x, @x)
SET @x = @x + 1
END
SELECT * FROM #Tempplaydate
DROP TABLE #Tempplaydate

i wish to have result group by week according to the calender, if there are only 5 week, week6 should be NULL
example for the month of august
week1, week2, week3, week4, week5, week6
21, 105, 154, 203, 252, 40


Hope can help...but advise to wait pros with confirmation...

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 02:54:07
your definition of week starts from which day ? Sun ? Mon ? ? ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 03:03:08
my defination of week is according to calender as u see
august
  |  |  |  |  | 1| 2|<- week1
3| 4| 5| 6| 7| 8| 9|<- week2
10|11|12|13|14|15|16|<- week3
17|18|19|20|21|22|23|<- week4
24|25|26|27|28|29|30|<- week5
30| | | | | | |<- week6

as you can see each month maybe different


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 03:12:30
not everybody's calendar starts from Monday


select weekno = (datediff(day, '17530101', date1) / 7) - (datediff(day, '17530101', '2009-08-01') / 7) + 1,
sum(quantity)
from #Tempplaydate
where date1 >= '2009-08-01'
and date1 <= '2009-08-31'
group by datediff(day, '17530101', date1) / 7



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 03:20:58
quote:
'17530101'

what is this series of number means?


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 03:24:13
it is date in YYYYMMDD. It's a Monday


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 03:33:25
i think i get it.....where you learn all this thing...btw the following are half done by me.....doesn't know how to make the week number become nice...haha
SELECT	weekno, SUM(quantity)
FROM (
select row_number() over ( order by date1)-convert(int, date1)%7 as weekno,
quantity
from #Tempplaydate
)a
GROUP BY weekno



Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 03:40:43
quote:
where you learn all this thing

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762
http://weblogs.sqlteam.com/
http://www.sqlteam.com/article/datediff-function-demystified

Happy reading




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 03:42:05
yer....always throw me to sleep


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 03:43:02
quote:
Originally posted by waterduck

i think i get it.....where you learn all this thing...btw the following are half done by me.....doesn't know how to make the week number become nice...haha
SELECT	weekno, SUM(quantity)
FROM (
select row_number() over ( order by date1)-convert(int, date1)%7 as weekno,
quantity
from #Tempplaydate
)a
GROUP BY weekno


this query does not gives you what you want. The weekno is wrong


Hope can help...but advise to wait pros with confirmation...




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 03:44:57
ya i know....i just trying to solve it...but i think i can't make it
ps. the link you gave me really useful thx alot mr.tan


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 03:52:48
this will be able to cater for any year / month

;with data (weekno, mth, date1, quantity)
as
(
select weekno = datediff(day, '17530101', date1) / 7,
mth = dateadd(month, datediff(month, 0, date1), 0),
date1,
quantity
from #Tempplaydate
)
select [Year] = year(mth),
[Month] = month(mth),
[Week] = row_number() over (partition by min(mth) order by weekno),
[Sum] = sum(quantity)
from data
group by mth, weekno



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-07-22 : 05:01:42
Hi, Try this also using dynamic pivot.


DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME

SELECT @STARTDATE = '1/1/2009'
SELECT @ENDDATE = '12/31/2009'

DECLARE @TEMP TABLE ( DATE DATETIME )
INSERT INTO @TEMP
SELECT DATEADD(DAY, NUMBER, @STARTDATE)
FROM MASTER..SPT_VALUES
WHERE [TYPE] = 'P'
AND DATEADD(DAY, NUMBER, @STARTDATE) <= @ENDDATE

CREATE TABLE #DETAILS ( [MONTH] VARCHAR(32), DAY_NAME VARCHAR(32), [DAY] INT, MONTH_NUM INT ,WEEK_NUM INT,[WEEKDAY] INT)
INSERT INTO #DETAILS
SELECT DATENAME(M,DATE),DATENAME(DW,DATE), DAY(DATE), MONTH(DATE),DATEPART(WW,DATE),DATEPART(DW,DATE) FROM @TEMP

DECLARE @STRCOLS VARCHAR(MAX),@VALUES VARCHAR(MAX),@STRING VARCHAR(MAX)
SELECT @STRCOLS = '',@VALUES = '' ,@STRING = ''

SELECT @STRCOLS = @STRCOLS + ', MAX([' + CAST([WEEKDAY] AS VARCHAR(32)) + ']) AS '+ DAY_NAME,
@VALUES = @VALUES + ', ['+ CAST([WEEKDAY] AS VARCHAR(32)) +']'
FROM
( SELECT DISTINCT TOP 100 PERCENT [WEEKDAY],DAY_NAME FROM #DETAILS ORDER BY [WEEKDAY])T

SELECT @STRING = '
SELECT
Month' + @STRCOLS+'
FROM #DETAILS
PIVOT(MAX(DAY) FOR [WEEKDAY] IN (' + STUFF(@VALUES,1,1,'') + '))P
GROUP BY WEEK_NUM,[MONTH],MONTH_NUM
ORDER BY MONTH_NUM'

PRINT(@STRING)

EXEC (@STRING)

Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-07-22 : 05:04:31
(or) Try this also


DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME

SELECT @STARTDATE = '1/1/2009'
SELECT @ENDDATE = '12/31/2009'

DECLARE @TEMP TABLE ( DATE DATETIME )
INSERT INTO @TEMP
SELECT DATEADD(DAY, NUMBER, @STARTDATE)
FROM MASTER..SPT_VALUES
WHERE [TYPE] = 'P'
AND DATEADD(DAY, NUMBER, @STARTDATE) <= @ENDDATE

CREATE TABLE #DETAILS ( [MONTH] VARCHAR(32), DAY_NAME VARCHAR(32), [DAY] INT, MONTH_NUM INT ,WEEK_NUM INT,[WEEKDAY] INT)
INSERT INTO #DETAILS
SELECT DATENAME(M,DATE),DATENAME(DW,DATE), DAY(DATE), MONTH(DATE),DATEPART(WW,DATE),DATEPART(DW,DATE) FROM @TEMP

SELECT [Month],MAX([1]) AS 'Sunday', MAX([2]) AS 'Monday', MAX([3]) AS 'Tuesday',MAX([4]) AS 'Wednesday',
MAX([5]) AS 'Thursday',MAX([6]) AS 'Friday', MAX([7]) AS 'Saturday'
FROM ( SELECT * FROM #DETAILS) P
PIVOT ( MAX([DAY]) FOR [WEEKDAY] IN ([1],[2],[3],[4],[5],[6],[7]))P
GROUP BY WEEK_NUM,[MONTH],MONTH_NUM
ORDER BY MONTH_NUM
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-07-22 : 05:16:42
thx Nageswar9 ^^


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-07-22 : 05:26:30
quote:
Originally posted by waterduck

thx Nageswar9 ^^


Hope can help...but advise to wait pros with confirmation...



welcome
Go to Top of Page
   

- Advertisement -