Hello everyone, and Thanks in Advance.
I have a query that has me stuck, unless I use a cursor and I believe there has to be a better way than that.
I have a table that holds records of classes, I need to run a query that returns one class for each month after a given date.
So if my 1st table contains:
CourseCode CourseName
Math101 Pre-Algebra
Math201 Algebra
And I need to return 2 months of classes, determined by the statment:
select datediff(month, '2008-09-01', getdate())
Which returns a value of '2'.
Then I need the following result set:
CourseCode CourseName StartDate
Math101 Pre-Algebra 2008-09-01
Math101 Pre-Algebra 2008-10-01
Math201 Algebra 2008-09-01
Math201 Algebra 2008-10-01
I figured I could do a cartesian join off a temp table that contains all the dates, but I can't figure out how to create that table without using a cursor.
By the way, this will be an infrequent query, run rarely. So performance isn't a major consideration. But I don't want to create bad code either.
Any great thoughts on this one?
Thanks!
Patrick
I have a query that has me stuck, unless I use a cursor and I believe there has to be a better way than that.
I have a table that holds records of classes, I need to run a query that returns one class for each month after a given date.
So if my 1st table contains:
CourseCode CourseName
Math101 Pre-Algebra
Math201 Algebra
And I need to return 2 months of classes, determined by the statment:
select datediff(month, '2008-09-01', getdate())
Which returns a value of '2'.
Then I need the following result set:
CourseCode CourseName StartDate
Math101 Pre-Algebra 2008-09-01
Math101 Pre-Algebra 2008-10-01
Math201 Algebra 2008-09-01
Math201 Algebra 2008-10-01
I figured I could do a cartesian join off a temp table that contains all the dates, but I can't figure out how to create that table without using a cursor.
By the way, this will be an infrequent query, run rarely. So performance isn't a major consideration. But I don't want to create bad code either.
Any great thoughts on this one?
Thanks!
Patrick