How do I create a series of specific days in a SQL table? -


i have table called classtimes has column of type bool each day of week (sunday,monday,tuesday...etc) , contains startdate , enddate column. example, have row has monday , wednesday selected startdate of june 3, 2013 , enddate of june 26, 2013.

what i'd able create sproc insert series of rows table called classcalendar contains of dates between startdate , enddate each selected day column.

for example, if user selected monday , wednesay classtimes table, generate:

 row    date 1      2013-06-03 2      2013-06-10 3      2013-06-17 4      2013-06-24 5      2013-06-05 6      2013-06-12 7      2013-06-19 8      2013-06-26 

i have tried set bit on head. appreciated.

here's sql statement might work you.

select course,        convert(varchar(25), course_date, 101) course_date   (select course_no course,                dateadd(dd, rn-1, startdate) course_date,                datename(dw, dateadd(dd, rn-1, startdate)) dow           (select row_number() on (order c1) rn                   dummy) sub1,                classtimes          rn <= (datediff(dd, startdate, enddate)+1)        ) list_of_dates,        (select course_no,                (case monday when 1 'monday' end) dow           classtimes         union         select course_no,                (case tuesday when 1 'tuesday' end)           classtimes         union         select course_no,                (case wednesday when 1 'wednesday' end)                     classtimes         union         select course_no,                (case thursday when 1 'thursday' end)           classtimes         union         select course_no,                (case friday when 1 'friday' end)           classtimes         union         select course_no,                (case saturday when 1 'saturday' end)           classtimes         union         select course_no,                (case sunday when 1 'sunday' end)           classtimes         ) class_days  list_of_dates.dow = class_days.dow    , list_of_dates.course = class_days.course_no  order course_no,           course_date 

i used query

select course_no course,        dateadd(dd, rn-1, startdate) course_date,        datename(dw, dateadd(dd, rn-1, startdate)) dow   (select row_number() on (order c1) rn           dummy) sub1,        classtimes  rn <= (datediff(dd, startdate, enddate)+1) 

to generate list of dates between startdate , enddate each course. in order query work correctly, dummy table must contain @ least many rows number of days between startdate , enddate each course. result of list of days between startdate , enddate each course, along day of week date.

| course | course_date |       dow | ------------------------------------ |   math |  06/03/2013 |    monday | |   math |  06/04/2013 |   tuesday | |   math |  06/05/2013 | wednesday | |   math |  06/06/2013 |  thursday | |   math |  06/07/2013 |    friday | ......... ......... |   math |  06/24/2013 |    monday | |   math |  06/25/2013 |   tuesday | |   math |  06/26/2013 | wednesday | 

i have subquery uses series of unions take day of week columns classtimes table , generate days of week classes held. join list_of_dates subquery class_days subquery dates classes held.

|  course | course_date | ------------------------- |    math |  06/03/2013 | |    math |  06/05/2013 | |    math |  06/10/2013 | |    math |  06/12/2013 | |    math |  06/17/2013 | |    math |  06/19/2013 | |    math |  06/24/2013 | |    math |  06/26/2013 | 

i'm sure there's more efficient/elegant way generate list of days of week class classtimes table (the class_days subquery), couldn't think of one.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -