Skip to content
Advertisement

SQL Query to Update the Start and End Date in a table from a different column where the dates do not match

I need a query which will Update the Start_Date and End_Date in this table. By Checking the Mapping column.When the value of date starts to change that should become the new start and end date.

For example :

The above two are same till the part 25-NOV-15-15-AUG-22. So our new Start_date for 1st record should be =12-SEP-17 and End_date should be =20-SEP-21. For second record Start_date =20-SEP-21 and 15-AUG-22.

Please let me know if any clarification needed. I’ll try my best to explain.

Below are the table scripts and insert statements.

Advertisement

Answer

You can use:

Which, for your sample data, then:

Outputs:

COMPONENT_ID START_DATE END_DATE
17644 25-NOV-15 15-AUG-22
17644 27-JUN-16 15-AUG-22
18027 25-NOV-15 15-AUG-22
18027 27-JUN-16 15-AUG-22
18028 25-NOV-15 15-AUG-22
18028 27-JUN-16 15-AUG-22
22158 25-NOV-15 15-AUG-22
22158 27-JUN-16 15-AUG-22
116356 25-NOV-15 15-AUG-22
116356 27-JUN-16 15-AUG-22
116441 25-NOV-15 15-AUG-22
116441 27-JUN-16 15-AUG-22
116442 25-NOV-15 15-AUG-22
116442 27-JUN-16 15-AUG-22
116443 25-NOV-15 15-AUG-22
116443 27-JUN-16 15-AUG-22
122703 25-NOV-15 15-AUG-22
122703 27-JUN-16 15-AUG-22
123517 25-NOV-15 15-AUG-22
123517 27-JUN-16 15-AUG-22
123628 25-NOV-15 15-AUG-22
123628 27-JUN-16 15-AUG-22

db<>fiddle here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement