Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Calculate weekend Rate Topic Display Mode Topic Options
Author
 Message
 Posted Sunday, December 02, 2012 8:54 AM
 SSC-Insane Group: General Forum Members Last Login: Today @ 11:14 AM Points: 22,086, Visits: 28,994
 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)?
Post #1391732
 Posted Monday, December 03, 2012 3:21 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, December 04, 2013 8:00 PM Points: 1,725, Visits: 2,017
 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 03, 2012 3:22 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, December 04, 2013 8:00 PM Points: 1,725, Visits: 2,017
 I have created this script but looking for more optimization regarding performance--DECLARE @count intSET @count = 30DECLARE @sundays intSET @sundays = 0DECLARE @startdate datetimeSET @startdate ='2012-01-01'DECLARE @enddate datetimeSET @enddate = DATEADD(DD,@count,@startdate)PRINT @enddate PRINT 'Before Sunday'WHILE @startdate <= @enddateBEGINIF DATEPART(DW,@startdate) = 1BEGINSET @sundays = @sundays + 1SET @enddate = DATEADD(DD,1,@enddate)PRINT @enddatePRINT 'After Sunday'PRINT @sundaysENDSET @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 03, 2012 3:51 AM
 SSC Eights! Group: General Forum Members Last Login: Wednesday, November 20, 2013 2:57 AM Points: 854, Visits: 2,292
 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.aspxTo 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 03, 2012 5:45 AM
 SSC Journeyman Group: General Forum Members Last Login: Thursday, November 14, 2013 1:05 AM Points: 96, Visits: 277
 Anyone know if Joe's querySELECT (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 03, 2012 5:49 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 8:52 AM Points: 34,540, Visits: 28,708
 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.aspxTo 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.aspxJason,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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1391875
 Posted Monday, December 03, 2012 5:51 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, December 04, 2013 8:00 PM Points: 1,725, Visits: 2,017
 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 03, 2012 5:58 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 8:52 AM Points: 34,540, Visits: 28,708
 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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1391884
 Posted Monday, December 03, 2012 6:09 AM
 SSC 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 periodSELECT cal_date FROM CalendarWHERE julian_business_nbr = (SELECT julian_business_nbr + 30 FROM Calendar WHERE cal_date = '2007-04-05')
Post #1391892
 Posted Monday, December 03, 2012 6:51 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, November 27, 2013 10:01 AM Points: 162, Visits: 646
 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.xlsxHTH,B
Post #1391915

 Permissions