I have an audit table per_addresses_f_
address_id effective_start_date effective_end_date last_update_date audit_change_bit_map_ audit_action_type_ address_line_2
300000009360612 2000-03-27 2020-03-31 2020-07-06 00100000000000000 Update
300000009360612 2020-04-01 4712-12-31 2020-07-06 01101100000000000 Update Street12
300000009360612 2000-03-27 4712-12-31 2020-03-13 0 History
300000009360612 2000-03-27 2020-03-31 2020-03-13 0 History
I have used the below query to find out the change in address_line_2
select * from (
select 'Address' as Object
, address_id s1
, effective_start_date s2
, effective_end_date s3
, (select max(person_id) from PER_PERSON_ADDR_USAGES_F_ pauf where pauf.address_id = t.address_id) s4
, last_update_date s8
,audit_change_bit_map_ s9
,case when audit_action_type_ = 'UPDATE' then
case substr(audit_change_bit_map_, 2, 2) when '11' then 'Update'
when '00' then 'Correction'
when '01' then 'Update' end
when audit_action_type_ = 'HISTORY' and creation_date = last_update_date then 'Creation'
when audit_action_type_ = 'HISTORY' and creation_date <> last_update_date then 'History'
when audit_action_type_ = 'INSERT' then 'New'
when audit_action_type_ = 'DELETE' then 'Deleted' end Audit_Action
, 'Address Line 2' change
, ADDRESS_LINE_2 current_value
, LAG(t.address_line_2, 1,0) OVER (partition by (select max(person_id) from PER_PERSON_ADDR_USAGES_F_ pauf where pauf.address_id = t.address_id), address_id ORDER BY last_update_date) Prev_value
from fusion.per_addresses_f_ t
where address_id = 300000009360612
order by last_update_date desc)
In the above query the LAG function is not working correctly, if the previous value for the column is null, then 0 should be returned, but it not returning anything. The output the above query is giving -
address_id effective_start_date effective_end_date last_update_date audit_change_bit_map_ audit_action_type_ CURRENT_VALUE prev_value
300000009360612 2000-03-27 2020-03-31 2020-07-06 00100000000000000 Update
300000009360612 2020-04-01 4712-12-31 2020-07-06 01101100000000000 Update Street12
300000009360612 2000-03-27 4712-12-31 2020-03-13 0 History 0
300000009360612 2000-03-27 2020-03-31 2020-03-13 0 History
so when i add the condition in the above query where current_value <> prev_value
, it is not returning anything. Ideally it should have returned-
address_id effective_start_date effective_end_date last_update_date audit_change_bit_map_ audit_action_type_ CURRENT_VALUE prev_value
300000009360612 2020-04-01 4712-12-31 2020-07-06 01101100000000000 Update Street12 0
Is the lag function i am using incorrect?
You provided just part of your data: I see just 1 value in the address_line_2 - line #2 You can use simple lag without subqieries:
lag(ADDRESS_LINE_2) over(partition by address_id order by effective_start_date) Prev_value
Full test case on data you provided: (corrected a bit to make it clearer)
with per_addresses_f_(
address_id
,effective_start_date
,effective_end_date
,last_update_date
,audit_change_bit_map_
,audit_action_type_
,address_line_2 -- just 1 'Street12' in your example, so I filled other values to make it more clear
,creation_date -- you didn't provide this field, so I filled it with sysdate
) as (
select 300000009360612 , date'2000-03-27' , date'2020-03-31', date'2020-07-06' , '00100000000000000', 'Update ','Street11',sysdate from dual union all
select 300000009360612 , date'2020-04-01' , date'4712-12-31', date'2020-07-06' , '01101100000000000', 'Update ','Street12',sysdate from dual union all
select 300000009360612 , date'2000-03-27' , date'4712-12-31', date'2020-03-13' , '0 ', 'History','Street13',sysdate from dual union all
select 300000009360612 , date'2000-03-27' , date'2020-03-31', date'2020-03-13' , '0 ', 'History','Street14',sysdate from dual
)
select * from (
select 'Address' as Object
, address_id s1
, effective_start_date s2
, effective_end_date s3
-- , (select max(person_id) from PER_PERSON_ADDR_USAGES_F_ pauf where pauf.address_id = t.address_id) s4
, last_update_date s8
,audit_change_bit_map_ s9
,case when audit_action_type_ = 'UPDATE' then
case substr(audit_change_bit_map_, 2, 2) when '11' then 'Update'
when '00' then 'Correction'
when '01' then 'Update' end
when audit_action_type_ = 'HISTORY' and creation_date = last_update_date then 'Creation'
when audit_action_type_ = 'HISTORY' and creation_date <> last_update_date then 'History'
when audit_action_type_ = 'INSERT' then 'New'
when audit_action_type_ = 'DELETE' then 'Deleted' end Audit_Action
, 'Address Line 2' change
, ADDRESS_LINE_2 current_value
-- , LAG(t.address_line_2, 1,0) OVER (partition by (select max(person_id) from PER_PERSON_ADDR_USAGES_F_ pauf where pauf.address_id = t.address_id), address_id ORDER BY last_update_date) Prev_value
, lag(ADDRESS_LINE_2) over(partition by address_id order by effective_start_date) Prev_value
from fusion.per_addresses_f_ t
where address_id = 300000009360612
order by last_update_date desc)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.