qq997404392
伪墨
采纳率61.8%
2019-03-07 16:32

oracle根据查询条件、时间降序排序查询出最新的一条数据并更新

已采纳

update TBL_SQZL
set HYJG = 1
where ID = (
select ID
from (
select ID
from TBL_SQZL
where JYTGM is not null
and XM = '郭玉珍'
and ZJHM = '310101194307254025'
and ZJLX = '01'
order by CRT_TIMESTAMP desc)
where ROWNUM = 1
);

select准确查出一条数据,整体执行的时候就一直转圈圈……表数据有两万四千多条,是需要优化还是怎么滴?

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

3条回答

  • qq997404392 伪墨 2年前

    又是靠自己。。
    通过Oracle的开窗函数over(partition by):

    update TBL_SQZL
    set HYJG = '1'
    where ID = (
      select t2.ID
      from (select t.*,
                   row_number() over (
                     PARTITION BY t.ZJHM
                     ORDER BY
                       t.CRT_TIMESTAMP DESC
                     ) as row_flg
            from TBL_SQZL t) t2
      where t2.JYTGM is not null
        and t2.XM = '郭玉珍'
        and t2.ZJHM = '310101194307254025'
        and t2.ZJLX = '01'
        and t2.row_flg = '1');
    
    点赞 评论 复制链接分享
  • qq_32210447 浅若挽歌 2年前
    update TBL_SQZL
    set HYJG = 1
    where JYTGM is not null
    and XM = '郭玉珍'
    and ZJHM = '310101194307254025'
    and ZJLX = '01'
    and CRT_TIMESTAMP >=(select max(b.CRT_TIMESTAMP) from TBL_SQZL b)
    
    点赞 评论 复制链接分享
  • weixin_43902689 /*空山*/ 2年前

    你子查询里都没有加top 1

    点赞 评论 复制链接分享