u010463375
u010463375
采纳率50%
2013-07-02 04:57 阅读 5.4k

求帮助,oracle数据库,查询出每个最新时间的那一条数据

select p.* from (select rownum as 序号 ,n.* from (select sitename as 站点名称, RecordTime as 记录时间 ,decode(avg(case item when 211 then value else null end),0,'0.00000',trim(to_char(avg(case item when 211 then value else null end),'99999999999990.99999'))) "SO2(mg/m3)" from(select s.sitename,m.* from air_temphourdata m left join site s on m.sitecode=s.sitecode where m.SiteCode in ('34656-31102001','34656-31102006'))group by sitename,Recordtime having to_char(Recordtime,'yyyymmddhhmmss')=MAX(to_char(Recordtime,'yyyymmddhhmmss')) order by RecordTime desc ) n )p where p.序号 between 1 and 15 只查询出每个站点最大时间的那一条

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • u010412440 com_jia 2018-03-16 07:28

    很简单的问题,使用窗口函数解决~
    select 站点名称,first_value("SO2(mg/m3)") over(partition by 站点名称 order by 记录时间 desc)
    from (
    select sitename as 站点名称,
    RecordTime as 记录时间,
    decode(avg(case item
    when 211 then
    value
    else
    null
    end),
    0,
    '0.00000',
    trim(to_char(avg(case item
    when 211 then
    value
    else
    null
    end),
    '99999999999990.99999'))) "SO2(mg/m3)"
    from (select s.sitename, m.*
    from air_temphourdata m
    left join site s
    on m.sitecode = s.sitecode
    where m.SiteCode in ('34656-31102001', '34656-31102006'))) n

    点赞 评论 复制链接分享

相关推荐