Question

My table holds type 2 change history; so if recordID 123 has a change made by a user, then 'latest_effective' gets set to FALSE and the newly created record with recordID 123 has 'latest_effective' set to TRUE. I'm looking for a query that returns the new record & the record that was just changed with a third row that is TRUE if there is a change in that column and FALSE if not.

Was it helpful?

Solution

select RecordID,1 latest_effective,Column1,Column2, etc...
from YourTable
where latest_effective = 1
union all
select RecordID,0 latest_effective,Column1,Column2, etc...
from YourTable
where latest_effective = 0
union all
select t.RecordID,-1 latest_effective,
    case t.Column1 when f.Column1 then 'FALSE' else 'TRUE' end AS Column1
    case t.Column2 when f.Column2 then 'FALSE' else 'TRUE' end AS Column2, etc...
from YourTable AS t
    inner join YourTable AS f
        on f.RecordID = t.RecordID
        and f.latest_effective = 0
where t.latest_effective = 1
order by RecordID,latest_effective desc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top