 select *
 from analytics_subscription_transitions b
 join audit_log al on b.subscription_event_record_id = al.target_record_id and al.change_type = 'INSERT' and table_name = 'SUBSCRIPTION_EVENTS'
-where coalesce(b.created_reason_code, 'NULL') != coalesce(al.reason_code, 'NULL')
-or coalesce(b.created_comments, 'NULL') != coalesce(al.comments, 'NULL')
-or coalesce(b.created_by, '') != coalesce(al.created_by, '')
+/* TODO Look at entilement rows only, ignore in-memory events and blocking states */
+where (coalesce(b.prev_service, 'entitlement-service') = 'entitlement-service'
+and coalesce(b.next_service, 'entitlement-service') = 'entitlement-service'
+and b.subscription_event_record_id is not null)
+and (coalesce(b.created_reason_code, 'NULL') != coalesce(al.reason_code, 'NULL')
+or coalesce(b.created_comments, 'NULL') != coalesce(al.comments, 'NULL')
+or coalesce(b.created_by, '') != coalesce(al.created_by, ''))
 ;
 
 

from analytics_account_transitions bos
left outer join blocking_states bs on bs.record_id = bos.blocking_state_record_id
where coalesce(bs.record_id, 'NULL') != coalesce(bos.blocking_state_record_id, 'NULL')
or coalesce(bs.TYPE, 'NULL') != 'SUBSCRIPTION_BUNDLE'
or coalesce(bs.state, 'NULL') != coalesce(bos.status, 'NULL')
or coalesce(bs.created_date, 'NULL') != coalesce(bos.created_date, 'NULL')
or coalesce(bs.created_date, 'NULL') != coalesce(bos.start_date, 'NULL')
or coalesce(bs.account_record_id, 'NULL') != coalesce(bos.account_record_id, 'NULL')
or coalesce(bs.tenant_record_id, 'NULL') != coalesce(bos.tenant_record_id, 'NULL')
;


-- A2
select *
from analytics_accounts b
join account_history ah on b.account_record_id = ah.target_record_id
join audit_log al on ah.record_id = al.target_record_id and al.change_type = 'INSERT' and al.table_name = 'ACCOUNT_HISTORY'
where coalesce(b.created_reason_code, 'NULL') != coalesce(al.reason_code, 'NULL')
or coalesce(b.created_comments, 'NULL') != coalesce(al.comments, 'NULL')
or coalesce(b.created_by, '') != coalesce(al.created_by, '')
