SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Not using loops - to loop
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

richardlaw
Yak Posting Veteran

United Kingdom
68 Posts

Posted - 05/04/2013 :  15:14:32  Show Profile  Visit richardlaw's Homepage  Reply with Quote
Hi

I have a table of classes. Each class runs only once a week, and the classes table stores the day it runs as an INT (1 = Mon, 2 = Tue, … 7 = Sun).

I now need to create a temp table that list all the classes that take place within a date range (like a calendar of events). So for example, if the first class runs even Monday, and my date range is over 4 weeks and 3 days, I would expect to see in my temp table 5 rows (date, classID) – assuming one of the 3 days is a Monday.

I assume I would need a loop of some kind that filters each date in the date range to check if a class runs on that day, if so, add a row to the temp table with the date and classID; if not, move on to the next class before moving on to the next day (date + 1 day).

I’d really like to find a way that does not require loops. The method above seems very process intensive. I’m new to TSQL, but looping like that just feels clumsy.

Any ideas?
Thanks as always

MuMu88
Aged Yak Warrior

547 Posts

Posted - 05/04/2013 :  23:06:53  Show Profile  Reply with Quote
Refer to
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185092 &
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185095
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/06/2013 :  02:03:12  Show Profile  Reply with Quote
The best thing in your case might be to use a calendar table as base and then join to your table based on the dates. Then you can filter on day field to give you only mondays

see

http://visakhm.blogspot.in/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000