Solved

Set start and end times in booking system

Posted on 2006-10-19
7
256 Views
Last Modified: 2012-05-05
I have the below query which pulls out free time slots based on the booked slots. However it does not factor in the start and end times if they are not already a booked apointment. So if the first appointment booked is at 10-11am, yet the engineer starts their day at 8am it dosent show that there is a free time from 8-10. It will only show a free apointment from 11am.

I have two tables Schedule and engineers. The engineers one will have their start and end time and the schedule will hold the booked appointments. They are linked by the engineer.

Tables: -
egineers: engineers, STARTTIME, ENDTIME
Scedule: IDNum, Engineer, FromTime, ToTime, BookedDate


SELECT DISTINCT
                      TOP 100 PERCENT S1.IDNum, S1.Engineer, S1.ToTime AS FromTime, S2.FromTime AS ToTime, S1.BookedDate, dbo.tbl_engineer.STARTTIME,
                      dbo.tbl_engineer.ENDTIME
FROM         dbo.Schedule S1 INNER JOIN
                      dbo.Schedule S2 ON S1.ToTime < S2.FromTime INNER JOIN
                      dbo.tbl_engineer ON S1.Engineer = dbo.tbl_engineer.ENGINEER
WHERE     (S2.FromTime =
                          (SELECT     MIN(x.Fromtime)
                            FROM          Schedule AS x
                            WHERE      x.FromTime > s1.ToTime)) AND (NOT EXISTS
                          (SELECT     x.Fromtime
                            FROM          Schedule AS x
                            WHERE      x.FromTime = s1.ToTime)) AND (S1.BookedDate >= CONVERT(DATETIME, '10/10/2006', 103))
ORDER BY S1.BookedDate, S1.FromTime
0
Comment
Question by:martgriff
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:dduser
Comment Utility
How do you store your starttime, endtime in Engineers & Schedule Table - FromTime, ToTime. I would require the format.

Regards,

dduser
0
 

Author Comment

by:martgriff
Comment Utility
Below is how i have stored the data in teh two tables that i am using. Thanks


Date in Schedule table: -

IDNum      FromTime      ToTime      BookedDate      Engineer
1      09:00:00      10:00:00      10/10/2006      Eng1  
2      12:00:00      13:00:00      10/10/2006      Eng2
3      15:00:00      17:00:00      10/10/2006      Eng2
4      08:00:00      10:00:00      11/10/2006      Eng1
5      12:00:00      13:00:00      11/10/2006      Eng1  
6      15:00:00      17:00:00      11/10/2006      Eng1


Data in engineer table: -

Engineer        STARTTIME         ENDTIME
Eng1                  08:00:00           17:00:00
                        
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
The code below handles the end-of-day times, plus corrects some bugs in the original code.  Still trying to figure out an efficient way to get the start-of-day unbooked time.


SELECT unbooked.Engineer, unbooked.BookedDate,
    unbooked.FromTime, ISNULL(unbooked.ToTime, eng.EndTime) AS ToTime
FROM (
    SELECT IDNum, Engineer, BookedDate, ToTime AS FromTime,
        (SELECT MIN(fromTime)
          FROM  schedule x
          WHERE x.engineer = s1.engineer
          AND   x.bookedDate = s1.bookedDate
          AND   x.fromTime >= s1.ToTime) AS ToTime
    FROM schedule s1
    WHERE bookedDate >= CONVERT(DATETIME, '10/10/2006', 103)
) AS unbooked
INNER JOIN tbl_engineer eng ON eng.engineer = unbooked.engineer
WHERE unbooked.FromTime <> unbooked.ToTime
OR    (unbooked.ToTime IS NULL AND unbooked.FromTime < eng.EndTime)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Most efficient is probably using a temp table to store the above result, then doing the final selection from the temp table.  But I wasn't sure if you were willing to use a temp table or not.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
I think this gets the start times, although it requires extra table scans and joins:


SELECT unbooked.Engineer, unbooked.BookedDate,
    unbooked.FromTime, ISNULL(unbooked.ToTime, eng.EndTime) AS ToTime
FROM (
    SELECT Engineer, BookedDate, ToTime AS FromTime,
        (SELECT MIN(fromTime)
          FROM  schedule x
          WHERE x.engineer = s1.engineer
          AND   x.bookedDate = s1.bookedDate
          AND   x.fromTime >= s1.ToTime) AS ToTime
    FROM schedule s1
    WHERE bookedDate >= CONVERT(DATETIME, '10/10/2006', 103)
    UNION ALL
    SELECT e2.Engineer, s2.BookedDate, e2.StartTime AS FromTime, MIN(s2.FromTime) AS ToTime
    FROM tbl_engineer e2
    INNER JOIN schedule s2 ON s2.engineer = e2.engineer
    WHERE s2.bookedDate >= CONVERT(DATETIME, '10/10/2006', 103)
    GROUP BY e2.Engineer, s2.BookedDate, e2.StartTime
    HAVING MIN(s2.FromTime) > e2.StartTime      
) AS unbooked
INNER JOIN tbl_engineer eng ON eng.engineer = unbooked.engineer
WHERE unbooked.FromTime <> unbooked.ToTime
OR    (unbooked.ToTime IS NULL AND unbooked.FromTime < eng.EndTime)
ORDER BY unbooked.BookedDate, unbooked.FromTime
0
 

Author Comment

by:martgriff
Comment Utility
Thanks that second one works really well and does exactly what i wanted it to do.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Np, it was a very interesting q.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now