【问题标题】:comparing current row and previous in oracle sql在oracle sql中比较当前行和上一行
【发布时间】:2015-05-04 11:08:13
【问题描述】:

我需要比较当前行和前一行,并且基于一些比较需要得出一个列值。目前我正在关注的方法是制作两个不同的记录集,然后使用排名函数,然后通过加入排名函数,我可以实现这一点。但是,这似乎是乏味的方法,有没有更好的方法来实现这一点。我目前正在编写如下查询:-

    select 
< comparing columns from two record sets and deriving column value> 
              (
        select(<some complex logic>, rank from a) rcdset, 
    (select <some complex logic>, rank +1 from a) rcdset2 where rcdset.rnk = rcdset1.rnk (+)

数据库 - Oracle 10g

【问题讨论】:

  • 使用分析函数LAG/LEAD.

标签: sql oracle


【解决方案1】:

使用LAG(value_expr) OVER (ORDER BY rank_col) 检索上一行的值(value_expr)(由rank_col 定义的顺序),请参阅http://oracle-base.com/articles/misc/lag-lead-analytic-functions.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多