Solved

Set start and end times in booking system

Posted on 2006-10-19
7
260 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
ID: 17766869
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
ID: 17766911
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:Scott Pletcher
ID: 17769841
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 17769854
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:
Scott Pletcher earned 500 total points
ID: 17769953
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
ID: 17772043
Thanks that second one works really well and does exactly what i wanted it to do.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 17773718
Np, it was a very interesting q.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

840 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