Hello SQL gurus,
I am trying to split some of the data from one row into multiple rows with conditions.
in the data set, the original value of ActionType could be Changed, Added, or deleted.
The condition of changes are:
[ul]
[li]If ActionType = Changed, we must split it into 2 rows : ActionType = Deleted and then ActionType = Added. The Deleted row will have IdentityCode = FromIdentityCode and the Added row will have IdentityCode = ToIdentityCode[/li]
[li]If ActionType = Added, we don't need to split the row, replace IdentityCode = ToIdentityCode.[/li]
[li]If ActionType = Deleted, we don't need to split the row, replace IdentityCode = FromIdentityCode.[/li]
[/ul]
Here are the examples:
from the original dataset is as below:
The expected result as per below:
Any idea on how to achieve this?
Thank you so much in advance,
I am trying to split some of the data from one row into multiple rows with conditions.
in the data set, the original value of ActionType could be Changed, Added, or deleted.
The condition of changes are:
[ul]
[li]If ActionType = Changed, we must split it into 2 rows : ActionType = Deleted and then ActionType = Added. The Deleted row will have IdentityCode = FromIdentityCode and the Added row will have IdentityCode = ToIdentityCode[/li]
[li]If ActionType = Added, we don't need to split the row, replace IdentityCode = ToIdentityCode.[/li]
[li]If ActionType = Deleted, we don't need to split the row, replace IdentityCode = FromIdentityCode.[/li]
[/ul]
Here are the examples:
from the original dataset is as below:
Code:
RowNumber ParentID ChildID ActionType 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
The expected result as per 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
Any idea on how to achieve this?
Thank you so much in advance,