Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to build an algorithm that can look at X number of records which have overlapping dates in them and organize and re-insert those date values in a chronological sequence.
E.g.:
The table has a record like this:

Start Date End Date Value
1/1/2019 12/31/2099 10

When a new record like this is inserted,
5/1/2019 5/31/2019 25

The algorithm should organize the records like this and insert it into a new table:
1/1/2019 4/30/2019 10
5/1/2019 5/31/2019 25
6/1/2019 12/31/2099 10

The original table should not be modified, i.e. no reinserting/reordering of records in that original table.

Any tips on the most efficient way to accomplish this? Not looking for database specific solutions.. programming only.
Thanks!

What I have tried:

Tried whiteboarding it, but it seemed like i'm ending up with way too many passes thru the data before coming close to a possible solution. Wasn't sure if I was missing a more obvious algorithm out there.
Posted
Updated 7-Apr-19 18:12pm

1 solution

Not sure if I understand the question correctly and if this is about RDBMS, but...

In majority of cases the data is not sorted in the table, but sorted upon fetching. The DBMS reuses free space and some DBMS may relocate rows when needed so depending on the solution sorting may be impossible to do on a physical level. Also there is no way to fetch the data using the physical order.

Based on your example, why not simply use ORDER BY clause using two fields. For example
SQL
SELECT ...
FROM ...
ORDER BY StartDate, EndDate
 
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