I have a very complex problem that I need some assistance with for anyone willing to tackle this. Here is the situation.
I have 4 relational tables (T1, T2, T3, T4). T1 is the parent table with the PK being v_seq_num. T3 has a pk of l_seq_num. T2 contains an update_date field that I need to key off of.
Relations:
T1 -> T2 by v_seq_num (0...n)
T3 -> T2 by l_seq_num (0...n)
T4 -> T2 by l_seq_num and v_seq_num
Rules:
- I need to find a T1 record that has multiple records in T2 (based on v_seq_num) and delete the older records in T2, T3, and T4 (based on the update_date in T2).
- The results must be packaged into a stored procedure
- Records in T1 will never be deleted only T2, T3, T4.
How is the best way to go about this problem? Here is an example of what I am dealing with
(This record will never be deleted)
T1.v_seq_num = 123
(delete this record and child records based on date)
T2.v_seq_num = 123
T2.l_seq_num = 999
T2.update_date = 3/10/2005
(delete this record and child records based on date)
T2.v_seq_num = 123
T2.l_seq_num = 888
T2.update_date = 5/10/2005
(This is the record I want to keep based on update_date)
T2.v_seq_num = 123
T2.l_seq_num = 777
T2.update_date = 11/10/2005
(delete this record and child records based on date)
T3.l_seq_num = 999
(delete this record and child records based on date)
T3.l_seq_num = 888
(This is the record I want to keep based on T2 update_date)
T3.l_seq_num = 777
(delete this record and child records based on date)
T4.v_seq_num = 123
T4.l_seq_num = 999
(delete this record and child records based on date)
T4.v_seq_num = 123
T4.l_seq_num = 888
(This is the record I want to keep based on T2 update_date)
T4.v_seq_num = 123
T4.l_seq_num = 777
Any help would be greatly appreciated. This seems like a very complex problem and I am still fairly new to SQL. Thanks in advance.
I have 4 relational tables (T1, T2, T3, T4). T1 is the parent table with the PK being v_seq_num. T3 has a pk of l_seq_num. T2 contains an update_date field that I need to key off of.
Relations:
T1 -> T2 by v_seq_num (0...n)
T3 -> T2 by l_seq_num (0...n)
T4 -> T2 by l_seq_num and v_seq_num
Rules:
- I need to find a T1 record that has multiple records in T2 (based on v_seq_num) and delete the older records in T2, T3, and T4 (based on the update_date in T2).
- The results must be packaged into a stored procedure
- Records in T1 will never be deleted only T2, T3, T4.
How is the best way to go about this problem? Here is an example of what I am dealing with
(This record will never be deleted)
T1.v_seq_num = 123
(delete this record and child records based on date)
T2.v_seq_num = 123
T2.l_seq_num = 999
T2.update_date = 3/10/2005
(delete this record and child records based on date)
T2.v_seq_num = 123
T2.l_seq_num = 888
T2.update_date = 5/10/2005
(This is the record I want to keep based on update_date)
T2.v_seq_num = 123
T2.l_seq_num = 777
T2.update_date = 11/10/2005
(delete this record and child records based on date)
T3.l_seq_num = 999
(delete this record and child records based on date)
T3.l_seq_num = 888
(This is the record I want to keep based on T2 update_date)
T3.l_seq_num = 777
(delete this record and child records based on date)
T4.v_seq_num = 123
T4.l_seq_num = 999
(delete this record and child records based on date)
T4.v_seq_num = 123
T4.l_seq_num = 888
(This is the record I want to keep based on T2 update_date)
T4.v_seq_num = 123
T4.l_seq_num = 777
Any help would be greatly appreciated. This seems like a very complex problem and I am still fairly new to SQL. Thanks in advance.