Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am reporting a educational domain teacher's master table . The teacher commences multiple lectures in one day and each batch has got a unique batchid, along with this the table logs lecture starttime lecture end time for their respective battches
Following is the schema of my table ('M/W/F/' means Monday Wednesday Friday & 'T/T/S'/ means Tuesday Thursday and Saturday)
CSS
ID TEACHERID STARTDATE  EXPECTEDENDDATE         DAYS    STARTTIME   ENDTIME
3   1254    2007-11-28  2008-01-21              M/W/F/  9:00 AM     10:00AM
4   1254    2008-02-20  2008-03-17             M/W/F/   11:30 AM    1:00PM
5   1315    2008-11-17  2008-12-17             T/T/S/   2:00 PM     3:30PM
6   1315    2008-11-05  2008-12-05            T/T/S/    12:00 AM    1:30PM
8   1155    2008-04-18  2008-04-30            M/W/F/    10:30 AM    12:00AM
9   1514    2008-04-01  2008-08-06            T/T/S/    12:30 PM    1:00PM


Consider 1254 teacher , he is taking a batch from 9:00 AM to 10:00AM
then 11:30 AM to 1:00PM so he has 10:00AM to 11:30 AM free slot . How do I fetch it for each teacher if I give teacherID as input paramater .
I want to display the report in Asp.net front end page.
Posted
Comments
Thava Rajan 11-Nov-13 9:21am    
well what about the rest of the time does it not include
i mean for a single day what is the start time and what is the end of the school other wise the report might not show the correct result so will you provide that details little more
Kunal Ved 12-Nov-13 5:24am    
Each Teacher (Emloyee) has an assigned Shift Start and End time and their total working hour does not exceed 8.30 Hours.
That Particular's teachers shift timings are fetched from Employee Master Table which is mapped with the TeacherId column of the above diplayed table.

You need to reorganize your data quite a bit as its storage is over-complicating your problem
Days M/W/F -> you should have them listed separately. Simplest way is a single record for each day, remaining data duplicated.
Alternative: use the ID and have the work day distribution in a separate table by reference.

With that done, you can create (dynamically, or more easily, as a tmp table) a list for each day - individually

Now - lets pretend that you have your data reorganized for each item as individual days (a much larger table) so that each record now shows a single person on a single day with a single start and end time.

You can now sort them by date and time (are you beginning to see the strategy?)

Gaps can now be calculated using the SQL datetime DATEDIFF() function between adjacent records, comparing the end time of record n with the start time of record n+1. If > 0, then you have a gap (date is part of each record).

You will need to set up your boundary conditions for things such as start time for a day, end time for a day, days with no one currently assigned.

The key to many problems is an appropriate schema for your table(s) - thinking a bit more like a computer and not a human. Think how difficult it would be to actually use M/W/F vs M/W/Th in your calculations!

Once you fix your data organization, the most difficult part will be operating on adjacent rows.
 
Share this answer
 
Comments
Kunal Ved 12-Nov-13 8:02am    
Well if you think the data organisation is snag , i do not mind changing the structure as well , please suggest me , what would be the ideal way to store a periodical time table
W Balboos, GHB 12-Nov-13 8:26am    
Some examples/suggestions:

Conceptually easiest: instead of the field with m/t/w, you should have three records and an integer field to represent the day of the week. This still requires you break down the date ranges for those specific dates.

Can you afford to list each day separately, rather than as blocks? That will also simplify matters. It's much easier to do this as you create the data then to do the calculations (SQL case statements could help if this is not possible).

If times are always fixed on specific borders, I actually use a strategy of start-time and end-time as simple integer references to time slots (a room-reservation application in php/javascript w/SQL database). This showed available times to users by putting up a room, the time slots, and then disabling selection of slots hows numeric value is already within a range for that date/time/room. Every reservation is a single record, cannot span days and is forced to be contiguous by the storage logic (users are warned).

I have more records to handle but each one is conceptually simple: an owner reference, date, starting time block, ending time block. Blocks outside of the valid range do not exist for selection.

You can then use a subquery to create ranges that are NOT covered by the integer ranges, converting the results from integer references to time references for viewing. A data table mapping integers to start-times for fixed-length slots is very helpful for this.

And this is only one possible rearrangement -> others are possible. For example, if you put each day as its own record then you do not need to store the day of the week as it can be obtained from the SQL date functions.
SQL
DECLARE @tbl AS TABLE ( Id INT IDENTITY (1,1),Teacherid INT,StartTime DATETIME,EndTime DATETIME )

INSERT INTO @tbl
  (Teacherid, StartTime, EndTime)
VALUES
  (1254, '2007-11-29 09:00 AM','2007-11-29 10:00 AM'),
  (1254, '2007-11-29 11:30 AM','2007-11-29 1:00 PM'),
  (1254, '2007-11-30 09:00 AM','2007-11-30 10:00 AM'),
  (1254, '2007-11-30 11:30 AM','2007-11-30 1:00 PM'),
  (1254, '2007-12-01 09:00 AM','2007-12-01 10:00 AM'),
  (1254, '2007-12-01 11:30 AM','2007-12-01 1:00 PM')

SELECT t.EndTime, t2.StartTime
  FROM @tbl t LEFT JOIN @tbl t2 ON t2.Id = t.Id+1

please take close look at the table strucrure i have entered all days not just the interval
hope you understand and more over this is just a sample you may decide your own logic
 
Share this answer
 
Comments
Member 10807175 2-Dec-14 5:37am    
Excellent Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900