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.
| Author |
Topic |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-22 : 02:48:31
|
CREATE TABLE #Tempplaydate(date1 datetime,quantity int)DECLARE @x INTSET @x = 0WHILE @x <= 70BEGININSERT INTO #Tempplaydate VALUES (getdate()+@x, @x)SET @x = @x + 1ENDSELECT * FROM #TempplaydateDROP TABLE #Tempplaydatei wish to have result group by week according to the calender, if there are only 5 week, week6 should be NULLexample for the month of augustweek1, week2, week3, week4, week5, week621, 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] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-22 : 03:03:08
|
my defination of week is according to calender as u seeaugust | | | | | 1| 2|<- week1 3| 4| 5| 6| 7| 8| 9|<- week210|11|12|13|14|15|16|<- week317|18|19|20|21|22|23|<- week424|25|26|27|28|29|30|<- week530| | | | | | |<- week6 as you can see each month maybe different Hope can help...but advise to wait pros with confirmation... |
 |
|
|
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 #Tempplaydatewhere date1 >= '2009-08-01'and date1 <= '2009-08-31'group by datediff(day, '17530101', date1) / 7 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
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... |
 |
|
|
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] |
 |
|
|
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...hahaSELECT weekno, SUM(quantity)FROM ( select row_number() over ( order by date1)-convert(int, date1)%7 as weekno, quantity from #Tempplaydate )aGROUP BY weekno Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
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... |
 |
|
|
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...hahaSELECT weekno, SUM(quantity)FROM ( select row_number() over ( order by date1)-convert(int, date1)%7 as weekno, quantity from #Tempplaydate )aGROUP 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] |
 |
|
|
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 itps. the link you gave me really useful thx alot mr.tan Hope can help...but advise to wait pros with confirmation... |
 |
|
|
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 datagroup by mth, weekno KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-07-22 : 05:01:42
|
Hi, Try this also using dynamic pivot.DECLARE @STARTDATE DATETIMEDECLARE @ENDDATE DATETIMESELECT @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_VALUESWHERE [TYPE] = 'P' AND DATEADD(DAY, NUMBER, @STARTDATE) <= @ENDDATECREATE TABLE #DETAILS ( [MONTH] VARCHAR(32), DAY_NAME VARCHAR(32), [DAY] INT, MONTH_NUM INT ,WEEK_NUM INT,[WEEKDAY] INT)INSERT INTO #DETAILSSELECT DATENAME(M,DATE),DATENAME(DW,DATE), DAY(DATE), MONTH(DATE),DATEPART(WW,DATE),DATEPART(DW,DATE) FROM @TEMPDECLARE @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) |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-07-22 : 05:04:31
|
(or) Try this alsoDECLARE @STARTDATE DATETIMEDECLARE @ENDDATE DATETIMESELECT @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_VALUESWHERE [TYPE] = 'P' AND DATEADD(DAY, NUMBER, @STARTDATE) <= @ENDDATECREATE TABLE #DETAILS ( [MONTH] VARCHAR(32), DAY_NAME VARCHAR(32), [DAY] INT, MONTH_NUM INT ,WEEK_NUM INT,[WEEKDAY] INT)INSERT INTO #DETAILSSELECT DATENAME(M,DATE),DATENAME(DW,DATE), DAY(DATE), MONTH(DATE),DATEPART(WW,DATE),DATEPART(DW,DATE) FROM @TEMPSELECT [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) PPIVOT ( MAX([DAY]) FOR [WEEKDAY] IN ([1],[2],[3],[4],[5],[6],[7]))PGROUP BY WEEK_NUM,[MONTH],MONTH_NUMORDER BY MONTH_NUM |
 |
|
|
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... |
 |
|
|
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 |
 |
|
|
|
|
|
|
|