In our application we have one transaction table which contains huge data. So we decided to move old data to archive table based on the cutoff date provided(e.g. if cutoff date is 2013, then move all records before 2013 to archive table). We have following three tables Transaction_Details, Transaction_Headers and Bill_Mapping( I haven't included all tables and all columns):
Transaction_Details
table contains ID
(Transaction ID), Transaction_Header_ID
and other details.
Transaction_Headers
contains additional details about that transaction
(e.g. Payment_Type_ID
and GL_Date
(we want to compare cutoff with this column) etc.)
Bill_Mapping
table contains transaction mapping with other transactions (credit and debit) and Amount
.
I need a stored procedure which will return all transactions based on cutoff date provided (all transactions below cutoff date).
Example: Cutoff date is 2013-11-01 00:00:00.000
Now if you see in above Transaction_Details
table (screenshot), ID which are marked green have a GL_Date
(which is in the Transaction_Headers
table) less than cutoff date.
Now in Bill_Mapping
table, you can see transaction id 5434125 is linked to 5420422 (GL_Date : 2013-10-05) and 5415415 (GL_Date: 2013-12-01). So 5420422 is below cutoff date and 5415415 is above cutoff date. Here, I don't want to move these 3 transaction ids to archive table (because even though 5434125 and 5420422 is below cutoff date. but 5434125 is partially linked to 5415415 which is above cutoff date).
So rule is, based on bill_mapping
table even though if one transaction id is below cutoff date and partially mapped to any transaction which is above cutoff date. Then altogether we have don't have to move all linked transactions to archive table.