Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:46:29+00:00 2026-06-10T11:46:29+00:00

I have a query result like |———————————————-| |StaffId|BranchId|StartTime|EndTime |PatientId | |———————————————-| |1 |1 |09:30:00

  • 0

I have a query result like

|----------------------------------------------|
|StaffId|BranchId|StartTime|EndTime |PatientId |
|----------------------------------------------|
|1      |1       |09:30:00 |09:35:00|Null      |
|1      |1       |09:35:00 |09:40:00|Null      |
|1      |1       |09:40:00 |09:55:00|1         |
|1      |1       |09:55:00 |10:00:00|Null      |
|1      |1       |10:00:00 |10:05:00|Null      |
|1      |1       |10:05:00 |10:20:00|2         |
|1      |1       |10:20:00 |10:25:00|NULL      |
|1      |1       |10:25:00 |10:40:00|3         |
|1      |1       |10:40:00 |10:45:00|Null      |
|1      |1       |10:45:00 |10:50:00|Null      |
|1      |1       |10:50:00 |10:55:00|Null      |
|1      |1       |10:55:00 |11:00:00|Null      |
|----------------------------------------------|

but I want to be able to group all the unused timeslots (those without a Patient Id) so the result is

|----------------------------------------------|
|StaffId|BranchId|StartTime|EndTime |PatientId |
|----------------------------------------------|
|1      |1       |09:30:00 |09:40:00|Null      |
|1      |1       |09:40:00 |09:55:00|1         |
|1      |1       |09:55:00 |10:05:00|Null      |
|1      |1       |10:05:00 |10:20:00|2         |
|1      |1       |10:20:00 |10:25:00|NULL      |
|1      |1       |10:25:00 |10:40:00|3         |
|1      |1       |10:40:00 |11:00:00|Null      |
|----------------------------------------------|

Not really sure how to go about doing this though, any guidance would be appreciated. Not sure if it’s easier but the results of the query are got by joining a table with just the 5 minute intervals with the patients using the sql

INSERT INTO @Results(BranchId, StaffId, StartTime, EndTime)
SELECT us.BranchId, us.StaffId, us.StartTime, us.EndTime
FROM @UnusedSlots us
    left join @Results r
        on (NOT ((r.StartTime >= us.EndTime)
            OR (r.EndTime <= us.StartTime))) AND
            (us.StaffId = r.StaffId)
    where r.BranchId is Null

Where @UnusedSlots is the available 5 minute slots and @Results contains the booked slots (with the patient Ids). If there is a way to build the merging into this statement it would be much better.

I’ve thought I could use the Min(StartTime) and Max(EndTime) if I can isolate the adjoining results without a PatientId but I’m not sure how to do that, i keep ending up with one that has merged from 09:30 to 11:00

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:46:31+00:00Added an answer on June 10, 2026 at 11:46 am
    select 
        StaffId,
        BranchId,
        MIN(StartTime),
        MAX(EndTime),
        PatientId
    from
    (
    select *,       
        ROW_NUMBER() over (order by starttime) 
          - ROW_NUMBER() over (partition by isnull(PatientID,-1) order by starttime) rn     
    from @results
    ) v
    group by staffid, branchid, patientid, rn
    order by MIN (starttime)
    

    I’m not sure if StaffID and BranchID can vary in the @results table, so you may need to tweak this if they can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to retrieve an ordered query result, but I need to have
I have a query like below: Result gives @sold_count:=SUM(I.quantity) = 10, but @sold_count =
I have a query that creates a result set like this: Rank Name 1
If you have a simple Linq query like: var result = from record in
I have a result from a query like the below, which does not have
I have a query with a result like this No date No.PO product type
I have a query that produces a result like this: The data is sorted
I'm newbie to PHP and CodeIgniter. So I have a database query result like
when I have a query result for something like: select col as newName from
I have a view that generate the query result like below: channel | current_Month

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.