Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Week numbers and week start dates Expand / Collapse
Author
Message
Posted Tuesday, June 9, 2009 12:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Tuesday, June 9, 2009 12:58 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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
Posted Tuesday, June 9, 2009 7:26 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Tuesday, June 9, 2009 8:56 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 6:46 PM
Points: 36,944, Visits: 31,446
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 "Row-By-Agonizing-Row".

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #732000
Posted Tuesday, June 9, 2009 9:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Tuesday, June 9, 2009 10:34 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 6:46 PM
Points: 36,944, Visits: 31,446
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 "Row-By-Agonizing-Row".

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #732023
Posted Wednesday, June 10, 2009 8:06 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17, Visits: 31
thanks jeff.
Post #732712
Posted Wednesday, June 10, 2009 8:07 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 24, 2009 7:50 PM
Points: 17, Visits: 31
hi hitendar
any comments?

cheers
Post #732713
Posted Wednesday, June 10, 2009 8:39 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 6:46 PM
Points: 36,944, Visits: 31,446
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 "Row-By-Agonizing-Row".

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #732719
Posted Wednesday, June 10, 2009 11:38 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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 »

Add to briefcase 12»»

Permissions Expand / Collapse