分组取最大值统计,过滤

汇率保存,千分之5以内的,按照日期,币种,汇率,汇率浮动分组。
首先分组,按照日期,币种,汇率,汇率浮动,然后分组统计(日期,币种,汇率)= 1的并且,
浮动范围在千分5以内。

如果有2条汇率,一条记录大于千分之5,一条记录小于千分之5,分组统计数目就大于1,就过滤掉。
如果汇率正常的且有多条,然后取max 最大值。

   select c.trans_date, c.import_rate , c.settle_currency,c.txn_currency from (
  select b.trans_date,  max(b.import_rate) import_rate  , b.settle_currency,b.txn_currency ,rate_Flag,count(*) over (partition by trunc(trans_date),settle_currency,txn_currency) counts from (
      select a.trans_date,a.import_rate,a.settle_currency,a.txn_currency , case when to_number(dif) -0.005 >0 then '浮动汇率>0.005' else'浮动汇率正常' end rate_Flag 
      from ( select distinct dtl.inst_code, trunc(dtl.trans_date) trans_date , dtl.import_rate,dtl.settle_currency,txn_currency, exchange_rate calRate
             ,to_char(abs(dtl.import_rate  -exchange_rate )/dtl.import_rate,'99999990.990000') dif
             from tableA dtl
              where  batch_no = 'ABC_160314264' and txn_type !='PRCS' and txn_type !='PRRN') a
  ) b
group by trans_date ,settle_currency,txn_currency,rate_Flag
       ) c
where counts=1 and rate_Flag='浮动汇率正常'

猜你喜欢

转载自a545807638.iteye.com/blog/2282931