2014-04-15 46 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 
相關問題