Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
Development
»
Week numbers and week start dates
17 posts, Page 1 of 2
1
2
»»
Week numbers and week start dates
Rate Topic
Display Mode
Topic Options
Author
Message
balars_2000
balars_2000
Posted Tuesday, June 09, 2009 12:14 AM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17,
Visits: 31
Hi Folks,
Can you help me out with this?
I have a report that displays the week # and start of the week date and sums up revenue collected for the period.
For example, if I choose current year, Week # would start as 1 and start of the week should be 5/01 (first Monday of the year) and so on till current week which is 24 and week date is 08/06.
Thanks.
Post #731156
t.hitendra
t.hitendra
Posted Tuesday, June 09, 2009 12:58 AM
Old Hand
Group: General Forum Members
Last Login: Wednesday, October 17, 2012 10:33 AM
Points: 390,
Visits: 405
Hi there,
I have tried to write a query for your problem with this sample table, tblPurchaseOrder - PurchaseOrderDate & GrandTotal
here it is-
select weekno,newdate,sum(grandtotal) as grandtotal from
(select datepart(wk,purchaseorderdate) as weekno
,dateadd(dd,0,datediff(dd,0,purchaseorderdate-(datepart(weekday,purchaseorderdate)-1)+1)) as newdate,grandtotal
from tblpurchaseorder where purchaseorderdate<=getdate()) as tbl group by weekno,newdate
Here I have subtracted 1 from (weekday,purchaseorderdate) to
make it start from monday (default for monday will be 2).
and the whole equation gives monday of that week.
please try it and tell me if that is what you need.
Post #731183
balars_2000
balars_2000
Posted Tuesday, June 09, 2009 7:26 PM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17,
Visits: 31
hi mate
thanks for ur response; appreciate it.
sorry if i wasnt clear, i need week numbers and week start dates for any given date range or a year. if it was a year, for example 2009, the output would be
week # week start date
1 05/01
2 12/01
3 19/01
and so on till current date. if it was for a date range for example between 19/01 and 2/02 (for 2009) then it should be
week # week start date
1 19/01
2 26/01
3 02/02
thanks again for ur time :)
Post #731980
Jeff Moden
Jeff Moden
Posted Tuesday, June 09, 2009 8:56 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906,
Visits: 26,790
balars_2000 (6/9/2009)
hi mate
thanks for ur response; appreciate it.
sorry if i wasnt clear, i need week numbers and week start dates for any given date range or a year. if it was a year, for example 2009, the output would be
week # week start date
1 05/01
2 12/01
3 19/01
and so on till current date. if it was for a date range for example between 19/01 and 2/02 (for 2009) then it should be
week # week start date
1 19/01
2 26/01
3 02/02
thanks again for ur time :)
What do you want if data is missing for a whole week during the year?
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #732000
balars_2000
balars_2000
Posted Tuesday, June 09, 2009 9:33 PM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17,
Visits: 31
I would display 0's to indicate there was no data during that particular week.
--Ta
Post #732012
Jeff Moden
Jeff Moden
Posted Tuesday, June 09, 2009 10:34 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906,
Visits: 26,790
balars_2000 (6/9/2009)
I would display 0's to indicate there was no data during that particular week.
--Ta
Then you need a complete list of dates to join against for any given year...
DECLARE @FirstDate DATETIME
SELECT @FirstDate = '20090104'
SELECT v.Number AS Week,
DATEADD(wk, v.Number ,@FirstDate) AS StartDate,
DATEADD(wk, v.Number+1 ,@FirstDate) AS NextDate
FROM Master.dbo.spt_Values v
WHERE Type = 'P'
AND Number BETWEEN 0 AND 51
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #732023
balars_2000
balars_2000
Posted Wednesday, June 10, 2009 8:06 PM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17,
Visits: 31
thanks jeff.
Post #732712
balars_2000
balars_2000
Posted Wednesday, June 10, 2009 8:07 PM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17,
Visits: 31
hi hitendar
any comments?
cheers
Post #732713
Jeff Moden
Jeff Moden
Posted Wednesday, June 10, 2009 8:39 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906,
Visits: 26,790
balars_2000 (6/10/2009)
thanks jeff.
You bet. Thanks for the feedback. So, are you all set or do you have a remaining question?
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #732719
balars_2000
balars_2000
Posted Wednesday, June 10, 2009 11:38 PM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17,
Visits: 31
Jeff, I have a problem.
The query is working fine and listing all week numbers and dates.
However, I am interested in week day starting Mondays which is 05/01 for current year and subsequently 12/01, 19/01 etc..
Alternatively I should also be able to specify a date range and get the week start from there as well.
I have posted in detail in my previous messages.
Thanks for asking :)
Cheers
Post #732762
« Prev Topic
|
Next Topic »
17 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.