receiver_external_top5 : no duplicates

PHOTO EMBED

Wed Nov 16 2022 10:52:11 GMT+0000 (Coordinated Universal Time)

Saved by @shubhangi_burle

%jdbc(hive)
set tez.queue.name=default;
set hive.execution.engine=tez;

select DATE_SUB('{{next_ds}}',7) starting_date, DATE_SUB('{{next_ds}}',1) ending_date
,d.active_days as active_days
,'weekly' as date_range
,d.identifier as identifier
,'Rcv_External_users_top5' red_flag 
,d.value as value
,'INR' comment
,case when e.identifier is null then 'New' else 'Repeat' end as new_or_repeat 
,'AML' as `group`
,'FRA' as `type`
,'Alerts' as type_fra
,'User' as issue_type
,'UPI' as sub_issue_type
,'2022-11-11' as run_date from
(
  select a.active_days active_days
  ,a.identifier identifier
  ,a.value value
  from
  (
    select count(distinct updated_date) active_days
    ,receiveruser identifier
    ,sum(totaltransactionamount) Value
    from fraud.transaction_details_v3
    where updated_date BETWEEN DATE_SUB('2022-11-11',7) AND DATE_SUB('2022-11-11',1)
    and errorcode = 'SUCCESS'
    and receivertype = 'INTERNAL_USER'
    and workflowtype  = 'EXTERNAL_TO_CONSUMER'
    group by receiveruser 
    having Value>= 1000000
  ) a
  join
  (
    select count(distinct updated_date) active_days
    ,receiveruser identifier
    ,count(distinct transaction_id) Value
    from fraud.transaction_details_v3
    where updated_date BETWEEN DATE_SUB('2022-11-11',7) AND DATE_SUB('2022-11-11',1)
    and errorcode = 'SUCCESS'
    and receivertype = 'INTERNAL_USER'
    and workflowtype  = 'EXTERNAL_TO_CONSUMER'
    group by receiveruser
  ) b
  On a.identifier = b.identifier
  join
  (
    select count(distinct updated_date) active_days
    ,receiveruser identifier
    ,count(distinct sender_vpa) Value
    from fraud.transaction_details_v3
    where updated_date BETWEEN DATE_SUB('2022-11-11',7) AND DATE_SUB('2022-11-11',1)
    and errorcode = 'SUCCESS'
    and receivertype = 'INTERNAL_USER'
    and workflowtype = 'EXTERNAL_TO_CONSUMER'
    and totaltransactionamount >= 500
    and receivertype is not NULL
    group by receiveruser
    having Value >= 500) C
  On A.identifier = C.identifier
)d
left join 
    (select DISTINCT identifier from fraud.aml_freshdesk WHERE run_date < '2022-11-11') e 
On d.identifier = e.identifier
left join
    (select user_ext_id AS identifier
    , MAX(updated) as BLdate --
    from users.users 
    where blacklisted = 1 and blacklist_reason = 'SAM'
    GROUP BY user_ext_id HAVING BLdate < '2022-11-11'
    )f
On d.identifier = f.identifier
left join 
    (select DISTINCT identifier from fraud.aml_receiverside_temporary) g 
On d.identifier = g.identifier
where d.identifier is NOT NULL AND f.identifier is NULL AND g.identifier IS NULL
ORDER BY value DESC LIMIT 5
content_copyCOPY