2014-04-15 37 views
0

我的表格包含类型2更改历史;所以如果recordID 123有用户所做的更改,则'latest_effective'被设置为FALSE,并且具有recordID 123的新创建的记录将'latest_effective'设置为TRUE。 我正在查询返回新记录的查询&刚刚更改的记录,如果该列中存在更改,则第三行为TRUE,否则为FALSE。oracle sql - 比较第1行和第2行,在第3行显示结果?

回答

0
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 
相关问题