pivot table plus more issues -  need help with denormalizing data

Posted on 2007-11-24
Last Modified: 2012-05-05
I am building course calendar for some school. Everything is done except for the actual display of the calendar. I have to work on existing database structure -  meaning - I can add my tables and stored procs but can not change the ones that already exist.  On top of that - calendar display needs to be in the form of pivoted table (html table at that) that has the columns set to dates with subsets of time frames (morning, afternoon and evening) and rows set to class room  numbers.
The challenge lays in the way that data is stored and in the display format of calendar (pivot table).
Data is stored as follows:
course_id, section_id, semester, class_room, start_date, end_date, number_of_sessions, session_start_time, session_end_time, Mon, Tue, Wed, Thu, Fri, Sat, Sun

- Mon through Sun - contain the value set to 1 if class happens on that day of the week
- session_start_time and session_end_time are the start and end times for classes (ex. 8:00AM - 6:00PM)

My idea is to create another table based on existing date I described above that will have the following data in it:
course_id, section_id, semester, class_room, session_date, lesson_number, session_start_time, session_end_time, time_frame
 the problem I have is with mapping data from one table to another:
-  session_date -  needs to be a date calculated based on start_date, number_of_sessions or end_date, and Mon trough Sun values (if 1 then number of sessions advances)
- lesson_number -  i guess that would be the advancing session number from calculation of session_date
- time_frame -  well -  there I was planing to put morning, afternoon, evening, morning_afternoon, afternoon_evening, all_day -  that is based on the session_start_time and session_end_time. Time frames would be timed 9:00AM - 12:00PM, 12:00PM - 5:00PM, 5:00PM - 10:00PM ( session_start and end times could be something like 8:30AM - 6:30PM - so they do not correspond exactly to time frame intervals but fall within one of the intervals defined above.

So - how do I map that data and display it in the format I want?
I am out of ideas and would give 1 000 000  points if I only could to expert who helps me with that.
Ideally -  I would like to see as much ready solution as possible -  but I will really appreciate any pointers I can get.
Thanks in advance

Question by:lescluster
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 27

Expert Comment

ID: 20358747
Can you have more than one  course occupying the same classroom for different times within a subset of a day?

Author Comment

ID: 20358895
Mike - that's a YES
LVL 27

Expert Comment

ID: 20360144
You need a recordset that has one entry per course_id, per date, per day subset. It should have the Class_Room and the Start/End times. This would act as a base for your Crosstab, showing the Course_IDs at the intersection of the Class-ID rows and The Date/Day Subset columns.
You can get this with a Select statement. For that you'll need a 'helper' table, I usually call it Nums, with a single integer column, Num. Populate this with the integers from 0 to a number comfortably greater than the number of days in a Semester, one number per row. The basic idea of using Nums is to create several output rows where there is only one input row. So, to start with, if you want one row per Course_ID, by date for all the course instances on Mondays in a semester:
Assume two date parameters, @Start and @End for the semester.

Select C.*, DateAdd(d, Num, @Start) as CalDate
From tblCourses C, Nums
Mon = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 2
And Num Between 0 and DateDiff(d, @Start, @End)

This can be extended to the other days of the week, but I don't have time for that right now. I'll pick it up again tomorrow if you want to pursue this line of attack.

Hope this helps,

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!


Author Comment

ID: 20360173
Mike - let's continue -  I was thinking about it along the same lines.
LVL 27

Accepted Solution

MikeToole earned 500 total points
ID: 20360460
Just found an extra 5 mins. This, I think, adds the rest of the days of the week:

Select C.*, DateAdd(d, Num, @Start) as CalDate
From tblCourses C, Nums
Mon = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 2
Tue = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 3
Wed = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 4
Thu = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 5
Fri = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 6
Sat = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 7
Sun = 1 And DatePart(dw, DateAdd(d, Num, @Start)) = 1
And Num Between 0 and DateDiff(d, @Start, @End)

How should the course data display in the columns? Just once base on start time, or in each that it occurs?
Is the Start/End time printed with the course?

Author Comment

ID: 20360587
I would like to show course_id and section_id together with start and end times once that spans across as many time frames as it is affecting. I was also thinking about generating bars the length of time intervals and actually get rid of hard set time frames.
Start/end time are printed with the course/ section info .

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at (http://www.experts-ex…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question