BY MANISH KUMAR CHAUDHARY
Interview Question
SQL
Write SQL to merge events with
overlapping dates.
DIFFICULTY LEVEL :HARD
Question From
Ankit Bansal
Yotube Channel
PROBLEM STATEMENT
Write SQL to merge events with overlapping dates.
QUERY
QUERY EXPLANATION
1.With cte we are getting the required records
along with the previous end date using LAG
function.
Output we will get
hall_id start_date end_date previous_end_date
1 2023-01-13 2023-01-14 NULL
1 2023-01-14 2023-01-17 2023-01-14
1 2023-01-15 2023-01-17 2023-01-17
1 2023-01-18 2023-01-25 2023-01-17
2 2022-12-13 2022-12-17 NULL
2 2022-12-09 2022-12-23 2022-12-17
3 2022-12-01 2023-01-30 NULL
2.Now we are querying for the required records using
UNION. First SELECT query we have used so that we
get the records where
start_date<=previous_end_date OR
previous_end_date IS NULL
This will give output as
hall_id start_date end_date
1 2023-01-13 2023-01-17
2 2022-12-09 2022-12-23
3 2022-12-01 2023-01-30
3.Second SELECT query after UNION we using to get
the records WHERE start_date >previous_end_date.
This will help us to get those records which are not
within any criteria range.
Output for this will be
hall_id start_date end_date
1 2023-01-18 2023-01-25
FINAL OUTPUT
hall_id start_date end_date
1 2023-01-13 2023-01-17
1 2023-01-18 2023-01-25
2 2022-12-09 2022-12-23
3 2022-12-01 2023-01-30
QUERY FROM ANKIT SIR
QUERY EXPLANATION
1.With iterator_cte we are simply providing a
numbering to the records so that we can iterate
using the recursive cte.
Output for this will be
hall_id start_date end_date iterator_id
1 2023-01-13 2023-01-14 1
1 2023-01-14 2023-01-17 2
1 2023-01-15 2023-01-17 3
1 2023-01-18 2023-01-25 4
2 2022-12-09 2022-12-23 5
2 2022-12-13 2022-12-17 6
3 2022-12-01 2023-01-30 7
2.Now we are using rcte for recursively running the
query.
The first query before UNION is an anchor query that
will act as our first start from where we will begin
iterating.
Here we are flaging the record as 1 so that we can
group them together if they fulfill the criteria.
3. With UNION operator now we are iterating for all
records by joining the second cte with our first cte on
the basis of iterator_id and increasing it by 1 for every
iteration..
Here, we have used CASE WHEN to match the required
condition and flag them. After flaging them we
adding the flag we have already set that is 1. This we
are doing so that for second time when it is different
record we get 2 as the flag.
Output we get for this is as below
hall_id start_date end_date iterator_id flag
1 2023-01-13 2023-01-14 1 1
1 2023-01-14 2023-01-17 2 1
1 2023-01-15 2023-01-17 3 1
1 2023-01-18 2023-01-25 4 2
2 2022-12-09 2022-12-23 5 3
2 2022-12-13 2022-12-17 6 3
3 2022-12-01 2023-01-30 7 4
4.Now we simply querying for the records records
that is hall_id, MIN(start_date) and MAX(end_date)
for each id by merging the overlapping dates.
We are eliminating the overlapping dates by
grouping them on the basis of hall_id and flag that
we created to group the records.
hall_id start_date end_date
1 2023-01-13 2023-01-17
1 2023-01-18 2023-01-25
2 2022-12-09 2022-12-23
3 2022-12-01 2023-01-30
BY MANISH KUMAR CHAUDHARY
THANK YOU
"Success is not about being the best. It's
about always getting better."
Behdad Sami