oracle sql - 比较第1行和第2行,在第3行显示结果?

时间:2014-04-15 23:04:07

标签: sql oracle compare case

我的表格包含第2类更改历史记录;因此,如果recordID 123具有用户所做的更改,则'latest_effective'将设置为FALSE,并且具有recordID 123的新创建的记录将'latest_effective'设置为TRUE。 我正在寻找一个返回新记录的查询&刚刚更改了第三行的记录,如果该列有变化则为TRUE,否则为FALSE。

1 个答案:

答案 0 :(得分: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