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 ««123»»

Calculate weekend Expand / Collapse
Author
Message
Posted Sunday, December 2, 2012 8:54 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
Another question, startdate is 2012-12-02 (a Sunday) and you add 1 day, do you want 2012-12-03 or 2012-12-04? What if you add 0 days to the same date? Will you every add a negative number of days (i.e. go backwards from a given date)?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391732
Posted Monday, December 3, 2012 3:21 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
in this case it will be '2012-02-03'


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1391827
Posted Monday, December 3, 2012 3:22 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
I have created this script but looking for more optimization regarding performance--

DECLARE @count int
SET @count = 30
DECLARE @sundays int
SET @sundays = 0
DECLARE @startdate datetime
SET @startdate ='2012-01-01'
DECLARE @enddate datetime
SET @enddate = DATEADD(DD,@count,@startdate)
PRINT @enddate
PRINT 'Before Sunday'
WHILE @startdate <= @enddate
BEGIN
IF DATEPART(DW,@startdate) = 1
BEGIN
SET @sundays = @sundays + 1
SET @enddate = DATEADD(DD,1,@enddate)
PRINT @enddate
PRINT 'After Sunday'
PRINT @sundays
END
SET @startdate = DATEADD(DD,1,@startdate)
END




_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1391829
Posted Monday, December 3, 2012 3:51 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 9:31 AM
Points: 922, Visits: 2,524
Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.

Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.

See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx

To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1391836
Posted Monday, December 3, 2012 5:45 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, November 14, 2013 1:05 AM
Points: 96, Visits: 277
Anyone know if Joe's query

SELECT (C2.julian_business_nbr - C1.julian_business_nbr)
FROM Calendar AS C1, Calendar AS C2
WHERE C1.cal_date = '2007-04-05',
AND C2.cal_date = '2007-04-10';

causes a cross join before the complete WHERE clause is applied ?

Or are we just cross joining the 2 rows that would be the answer sets of 2 discreet queries, each using one of the phrases of the where clause?

Post #1391871
Posted Monday, December 3, 2012 5:49 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:57 PM
Points: 35,593, Visits: 32,184
Jason-299789 (12/3/2012)
Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.

Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.

See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx

To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx

Jason,

I know it sounds strange to those of us that use a Tally Table on a regular basis, but there are still a lot of folks that don't know what it is. You either have to explain, provide a link, or provide the full code (preferably some combination of those) for people that don't understand.


--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 #1391875
Posted Monday, December 3, 2012 5:51 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
Jeff is right...
I dont have any knowledge of Tally Table..



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1391878
Posted Monday, December 3, 2012 5:58 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:57 PM
Points: 35,593, Visits: 32,184
kapil_kk (12/3/2012)
Jeff is right...
I dont have any knowledge of Tally Table..


Please see the following article. A Tally Table (or Numbers table, as some call it) is a simple table of sequential integers used to replace a WHILE loop.
http://www.sqlservercentral.com/articles/T-SQL/62867/

Joe is correct though. A Calendar table (pretty obvious what that is) would do better here. I'm on my way to work so don't have the time to demo that kind of solution now but I'll check on this post when I get home.


--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 #1391884
Posted Monday, December 3, 2012 6:09 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, November 14, 2013 1:05 AM
Points: 96, Visits: 277
Using Joe's Calendar table, this might provide a way for the kapil to get to the answer he wants.
It removes the need to recursively check if you added another sunday in the extension of the date period

SELECT cal_date FROM Calendar
WHERE julian_business_nbr = (SELECT julian_business_nbr + 30 FROM Calendar WHERE cal_date = '2007-04-05')
Post #1391892
Posted Monday, December 3, 2012 6:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 3:22 AM
Points: 167, Visits: 691
I'm with CELKO - use a calendar table, then you can count your Sundays, weekends, Easter Mondays and whatever else you need between two dates - much much easier, and kind on processing too.

For an example calendar, you can check out:
http://www.kimballgroup.com/data-warehouse-and-business-intelligence-resources/data-warehouse-books/booksmdwt/ (go to Chapter 7—Design and Develop the ETL System; date dimension) - Direct Link for DDL and sample data:
http://www.kimballgroup.com/wp-content/uploads/2012/07/Ch07_Date_Dim_2000-2020.xlsx

HTH,

B

Post #1391915
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse