选择带领COLUMN MAX

时间:2017-09-25 18:50:10

标签: sql oracle

我有一个带有subselect内部连接的选择,依此类推。但是从这个结果来看,我希望它能为我带来OidBilling专栏中最大的记录。下面是select的示例和它带来的结果示例,以便更容易理解我想要的结果。

select address_block_uc
      ,address_complement_uc
      ,u.name_uc
      ,m.description_meter
      ,m.serial_number_meter
      ,lst.date_read date_read
      ,value_read
      ,tm.name_type_meter
      ,lst.concentrator
      ,lst.port 
      ,m.oid_uc
      ,m.oid_meter
      ,m.type_constant
      ,f.first_measur
      ,lst.id_system
      ,ib.read0
      ,ib.oid_individual_billing
      ,get_correction_factor( lst.id_system, m.oid_meter, m.oid_uc, lst.date_read) corr_factor
      ,get_correction_pulse( lst.id_system, lst.concentrator, lst.port, lst.date_read) corr_pulse
      ,nvl(f.first_measur,0) + ( i.value_read * m.type_constant ) leitura
from (select id_system
            ,concentrator
            ,port
            ,date_read
            ,m.oid_uc
            ,m.oid_meter
      from rs_individual_read i
          ,rs_uc u, rs_meter m
          ,rs_counters c
      where m.oid_meter = c.oid_meter
      and c.concentrator_counters = i.concentrator
      and c.port_counters = i.port
      and date_read between to_date ('01/03/2017 23:00:00','dd/MM/yyyy HH24:MI:SS') and to_date ('25/03/2017 23:00:00','dd/MM/yyyy HH24:MI:SS')
      and date_read between c.dt_init and c.dt_end
      and date_read between m.dt_init and m.dt_end
      and u.oid_system=i.id_system
      and m.oid_uc=u.oid_uc
      and i.id_system=531
      and m.oid_meter=69451
      group by id_system
              ,concentrator
              ,i.port
              ,date_read
              ,m.oid_uc
              ,m.oid_meter) lst
inner join rs_individual_read i 
on (i.id_system = lst.id_system 
    and i.date_read = lst.date_read
    and i.concentrator = lst.concentrator
    and i.port = lst.port)
inner join rs_counters c
on (c.concentrator_counters = lst.concentrator
    and c.port_counters = lst.port
    and lst.date_read between c.dt_init and c.dt_end)
inner join rs_meter m
on (m.oid_meter = c.oid_meter
    and m.oid_meter = lst.oid_meter
    and lst.date_read between m.dt_init and m.dt_end)
inner join rs_uc u
on (u.oid_system = lst.id_system 
and u.oid_uc = lst.oid_uc
and u.oid_uc = m.oid_uc)
inner join rs_type_meter tm
on (m.oid_type_meter = tm.oid_type_meter)
inner join rs_individual_billing ib
on (ib.serial_number = m.serial_number_meter)
left outer join rs_first_measurs f
on (f.oid_meter = m.oid_meter)
order by date_read desc
        ,address_block_uc
        ,address_complement_uc
        ,name_uc

结果:

DATE_READ   OID_INDIVIDUAL_BILLING  LEITURA                                                                 
20/03/2017 23:00    8500    800                                                                 
20/03/2017 23:00    8791    800                                                                 
20/03/2017 23:00    8628    800                                                                 
19/03/2017 23:00    8500    450,971                                                                 
19/03/2017 23:00    8628    450,971                                                                 
19/03/2017 23:00    8791    450,971                                                                 
19/03/2017 23:00    8791    450,653                                                                 
19/03/2017 23:00    8500    450,653                                                                 
19/03/2017 23:00    8628    450,653                                                                 
19/03/2017 12:00    8628    450,653                                                                 
19/03/2017 12:00    8791    450,653                                                                 

结果会在报告中带来一些重复的时间表,并且为了不重复,我希望结果只带来相同日期的列oid_individual_billing的更大数据,例如19/03年/ 2017 23:00hs我们有3个寄存器但是我想它只显示8791行,​​这是该记录的oid_individual_billing列的最大ID ...有人可以帮助我吗?

我已经对这些列进行了总结,以便于发布。

对不起我的谷歌翻译英语

韩国社交协会!

1 个答案:

答案 0 :(得分:0)

我已经回答并且有效,请按照以下方式帮助将来某人

 inner join (select serial_number, max(read0) read0 from rs_individual_billing group by serial_number) temp
    on (temp.serial_number = m.serial_number_meter)

TKS!