Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table on booking orders 
Bookings (order_no, user_id, booking_time,cancel_time,complete_time)
I try to write a query to return the order_no from all rows where customers made concurrent bookings (customer made a new booking before they completed or cancelled the previous booking).

Explanation
Customer A booked #000 at 1:11, and completed it at 1:25. 
Customer A booked #001 at 1:18, and completed it at 1:30. 
Customer A booked #002 at 5:30, and completed it at 6:10. 
Customer B booked #020 at 1:50, and completed it at 2:00. 
Customer B booked #021 at 5:35, and completed it at 6:05.
 
Only Customer A had a concurrent booking. The correct query would return order_no #000 and #001.


I need help with this, Please someone help me


What I have tried:

<pre>
SELECT a.user_id
FROM Bookings a JOIN Bookings b ON a.user_id = b.user_id AND a.order_no <> b.order_no
WHERE
    (b.booking_time  < a.complete_time OR b.booking_time < a.cancel_time) AND
    (b.complete_time > a.booking_time OR b.cancel_time> a.booking_time
-- the issue is some record have complete_time but cancel_time is null, while the others have cancel_time but complete_time is null
Posted
Updated 7-Dec-20 2:19am
v3

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
098828 5-Dec-20 4:05am    
I have added what I tried, I think the issue in the date criteria. some record have complete_time but cancel_time is null, while the others have cancel_time but complete_time is null.
Try:
SQL
SELECT
    *
FROM
    Bookings As a
WHERE
    Exists
    (
        SELECT 1
        FROM Bookings As b
        WHERE b.user_id = a.user_id
        And b.order_no != a.order_no
        And b.booking_time < IsNull(a.complete_time, a.cancel_time)
        And a.booking_time < IsNull(b.complete_time, b.cancel_time)
    )
;
Demo[^]
 
Share this answer
 

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