Results 1 to 6 of 6

Thread: Searching algorithm solution needed...

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    9

    Searching algorithm solution needed...

    Hello guys,

    I have a timetabling system and I need to find a way to find x number of days where there are no appointments for a given person. For example, say person A wants to take a 5 day holiday but they need to first see if there are five consecutive days where they have no appointments in the database, the person could enter the number of days and the code would presumably load all of their appointment dates (connect to database and query using SQL) and use some sort of search to find if indeed there are some days free.

    If anyone has any code or advice on this it would be greatly appreciated.

    Best,
    Ozos

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Searching algorithm solution needed...

    For what you described, I don't think performance would be much of an issue, so you could do the query as you described, put the values into a List(of Date), then iterate through the list checking the gap between each two dates. For that to work, the list would have to be sorted, but that would best be done in the query itself. Comparing two dates is simple enough, as you can just subtract the smaller from the larger, which gives you a TimeSpan, and a TimeSpan has a TotalDays property, which would give you the information you needed.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    9

    Re: Searching algorithm solution needed...

    That sounds like a great plan and makes perfect sense, appreciate the help, I will give it a go and see how I get on.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Searching algorithm solution needed...

    You can actually make the query return the number of days between "each" appointment.

    You SELECT the appt rows for a person and JOIN to the appt row for that person with the MIN() date that is GREATER than the date of that appt row.

    Code:
    Select A1.User,A1.ApptDate
                      ,A2.ApptDate "NextAppt"
                      , DateDiff(dd,A1.ApptDate,A2.ApptDate) "DiffDays"
       From Appt A1
       Left Join Appt A2 on A2.User=A1.User
                      and A2.Apptdate=(Select Min(AX.ApptDate) From Appt AX
                                         Where AX.User=A1.User and AX.ApptDate>A1.Apptdate)
       Where A1.User='smith'
    If a User can have several appt's on a given date it would be better to use a VIEW that collapses all those rows into a single DISTINCT USER, APPTDATE row.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    9

    Re: Searching algorithm solution needed...

    Quote Originally Posted by szlamany View Post
    You can actually make the query return the number of days between "each" appointment.

    You SELECT the appt rows for a person and JOIN to the appt row for that person with the MIN() date that is GREATER than the date of that appt row.

    Code:
    Select A1.User,A1.ApptDate
                      ,A2.ApptDate "NextAppt"
                      , DateDiff(dd,A1.ApptDate,A2.ApptDate) "DiffDays"
       From Appt A1
       Left Join Appt A2 on A2.User=A1.User
                      and A2.Apptdate=(Select Min(AX.ApptDate) From Appt AX
                                         Where AX.User=A1.User and AX.ApptDate>A1.Apptdate)
       Where A1.User='smith'
    If a User can have several appt's on a given date it would be better to use a VIEW that collapses all those rows into a single DISTINCT USER, APPTDATE row.
    Excellent, cheers guys. With a little tweek I got the SQL working. Clever stuff.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Searching algorithm solution needed...

    Quote Originally Posted by ozos View Post
    Excellent, cheers guys. With a little tweek I got the SQL working. Clever stuff.
    Once you get these tricks in your tool belt you have them for life!

    Glad you got a solution that worked!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width