Hi SQL guru,
We are trying to locate the client information that has been changed.
However, in our historical dataset, the change definition is deleted record and added record
Hence the original dataset is as below:
The good news is it has been sorted by row number and we would like to retrieve the Identity code whether it has been changed, added or deleted with information on parentID, childID and date.
The logic we are trying to achieve in pseudo-code is
CASE
WHEN (ParentID and ChildID are the same as previous row number) and (current row number ActionType is Added and previous row number ActionType is Deleted) then is CHANGE
ELSE original ActionType -- whether it is added or deleted
END AS New_ActionType
CASE
WHEN New_ActionType is CHANGE THEN date is the (Added date)
ELSE original Date
END AS New_ActionDate
Also, we need to know from what "From identity code" to "to identity code"
If ActionType is added means "From identity code" is null and "to identity code" is the current identity code
If ActionType is deleted means "From identity code" is the current identity code or blank and "to identity code" is NULL
Hence the output result is as below:
I am trying to play around with SQL below
But somehow it is getting too fiddly, hopefully, you guys guru could help me with this.
Thanks in advance,
We are trying to locate the client information that has been changed.
However, in our historical dataset, the change definition is deleted record and added record
Hence the original dataset is as below:
Code:
RowNumber ParentID ChildID IdentityCode ActionType Date
1 ABC 123 T Deleted 2024-05-01
2 ABC 123 P Added 2024-05-03
3 BCD 234 T Deleted 2024-05-05
4 CDE 345 P Deleted 2024-05-06
5 CDE 345 T Added 2024-05-07
6 DEF 456 P Added 2024-05-08
The logic we are trying to achieve in pseudo-code is
CASE
WHEN (ParentID and ChildID are the same as previous row number) and (current row number ActionType is Added and previous row number ActionType is Deleted) then is CHANGE
ELSE original ActionType -- whether it is added or deleted
END AS New_ActionType
CASE
WHEN New_ActionType is CHANGE THEN date is the (Added date)
ELSE original Date
END AS New_ActionDate
Also, we need to know from what "From identity code" to "to identity code"
If ActionType is added means "From identity code" is null and "to identity code" is the current identity code
If ActionType is deleted means "From identity code" is the current identity code or blank and "to identity code" is NULL
Hence the output result is as below:
Code:
RowNumber ParentID ChildID NewActionType Date FromIdentityCode ToIdentityCode
1 ABC 123 Change 2024-05-03 T P
2 BCD 234 Deleted 2024-05-05 T NULL
3 CDE 345 Change 2024-05-07 P T
4 DEF 456 Added 2024-05-08 NULL P
I am trying to play around with SQL below
Code:
SELECT * FROM
TABLE T1
LEFT OUTER JOIN TABLE T1 ON T1.RowNumber = T2.RowNumber +1
Thanks in advance,