In SQL, I have a table that looks like this:
I wanted to combine the values from ‘Value_New’ per ‘id_Parent_Record’ according to the ‘Seq_Number’ which means it should always starts from 1.
As an example, 637454609993200 has total of 4 movements for ‘Field_Changed’ = ‘Task_Workflow’ .
Expected output should be exactly like this:
Initial Referral -> IRU -> Adjustment Approved -> Settlement
This is the code where I am currently at:
select ac."id_Parent_Record"
,ac."Field_Changed"
,ac."Value_Previous"
,ac."Value_New"
,ac."Changed_Date"
,row_number() over (partition by "id_Parent_Record", "Field_Changed" order by "Changed_Date" asc) as "Seq_Number"
from public.api_changelog ac
where ac."Field_Changed" IN ('_id_Matter','Task_Workflow')
and ac."id_Parent_Record" = '637454609993200';
Expected output should have a new field named ‘Transitions’ which values should look like this per row for the account 637454609993200, which Field_Change is equal to Task_Workflow:
Initial Referral -> IRU -> Adjustment Approved -> Settlement
2
Answers
The
STRING_AGG
function should be able to accomplish this.Each
Value_New
should be delimited by' -> '
and it should be ordered by theChanged_Date
field. AGROUP BY
will need to be included.Output:
A CTE could be used to rejoin the table back to the original result set
For a typical data warehouse dimension table, the data would look like this. I’m not posting as an answer for you to accept, but I’m showing how it simplifies your query. No query needed.
One drawback from this data model compared to your data model is that when a column changes, a new column is added or a column is deleted in the operational database (upstream), the dimension table above would need to be rebuilt and data moved over with the new DDL.