Hi,
I need to write sql query for the following scenarios.
We have 3 financial systems (sys1, sys2, sys3) where a same transaction gets entered independently into each system OR entered into one and exported to the other system. Transaction need not be in all 3 systems. We need to create reconcile report to see the status of transaction and display if it is reconciled or not. For this, in our stored procedure we are pulling data from 3 systems into temp tables and using CTE and other logic marking each transaction status in each system temp table. One of the systems (eg sys1), we made it as source.
Sys1 temp table has both parent and child records and are distinguished using type. Sys2 and sys3 has only children records. When report is created, we are showing parent from sys1 temp table and children from new temp table where children status is updated based on availability of it in all 3 systems.
DECLARE @sys1 TABLE
(
ID int,
childID varchar(20),
ParentID varchar(20),
RecType decimal(3,0),
SettleDate smalldatetime,
Principal money,
Sys3ID varchar(16) NULL,
Sys2ID int NULL,
Status varchar(25) NULL
)
DECLARE @sys2 TABLE
(
TxID int PRIMARY KEY NOT NULL,
CommonTransactionID varchar(16),
SettleDate smalldatetime,
Par money,
Sys3ID varchar(16) NULL,
Sys1ChildID,
Sys1ParentID bigint
)
DECLARE @sys3 TABLE
(
Sys3ID varchar(16),
REFERENCE varchar(16),
VALUE_DATE datetime,
DIRECTION char(1),
AMOUNT money,
RecSource varchar(2)
)
Insert Into @sys1 (ID, childID, ParentID, RecType, SettleDate, Principal)
Select 172876, 217955, 217954, 100, 2015-03-01, 100.00
Union
Select 172877, 217956, 217955, 50, 2015-03-01, 15.00
Union
Select 172878, 217957, 217955, 50, 2015-03-01, 25.00
union
Select 172879, 217958, 217955, 50, 2015-03-01, 10.00
Union
Select 172880, 217959, 217955, 50, 2015-03-01, 10.00
union
Select 172881, 217960, 217955, 50, 2015-03-01, 40.00
Insert Into @sys2(TxID, Sys1ID, settleDate, Par)
Select 4336620, 217956, 2015-03-01, 15.00
Union
Select 4336621, 217957, 2015-03-01, 25.00
union
Select 4336613, 217958, 2015-03-01, 10.00
Union
Select 4336614, 217959, 2015-03-01, 10.00
union
Select 4336615, 217960, 2015-03-01, 40.00
Insert into @sys3(Sys3ID, Reference, Value_Date, Direction, Amount)
Select 1, 5654471 4336620, 2015-03-01, O, 15.00
Union
Select 2, 5654481 4336621, 2015-03-01, 'O',25.00
Union
Select 3, 5654491 4336613, 2015-03-01, 'O',10.00
Union
Select 4, 5654501 4336614, 2015-03-01, 'O',10.00
Union
Select 5, 5654511 4336615, 2015-03-01, 'O', 40.00
After going thru lot of other logic, final temp table will have only children with status assigned. The above temp table data is only for 1 scenario.
The following are status of children.
This is how status of children is determined:
- Not Settled All child records start as Not settled in temp tables.
- Settled when record exists in sys3 and other criteria is met.
- Partially settled record exists in sys3 and either in sys1 or sys2 or other criteria is not met.
- Reconciled child record should exist in all 3 systems and all criteria is match
- Mismatched record has wrong amount when compared in any of 2 systems.
**************** My Part below*******************
My part is to update the status of parent based on children status and parent amount must match sum of child amounts. If amounts dont match, then leave the status of parent as null.
Determining the status of parent:
- Not Settled None of children has yet settled.
- Settled All children are settled.
- Partially settled some of children are as settled OR 1+ children are partially settled.
- Reconciled All children are reconciled.
- Partially Reconciled some children are partially reconciled.
- Null 1 or more childen has a status of mismatched.
- AND sum of children amount should match parent amount
How can I update the status of parent based on all children and sum of amount of children equal to parent amount.
Thanks,
Spunny