开发者

compare current row value with next row value

开发者 https://www.devze.com 2023-01-26 05:56 出处:网络
ID ------ 1 1 2 2 3 4 5 5 5 6 7 7 7 8 9 9 10 9 I need to compare first row value with next row value. If they are equal display y in another column.
    ID
------
     1
     1
     2
     2
     3
     4
     5
     5
     5
     6
     7
     7
     7
     8
     9
     9
    10
     9

I need to compare first row value with next row value. If they are equal display y in another column.

    ID               flag
------            -------
     1                 y
     1                 n
     2                 y
     2                 开发者_Go百科n
     3                 n
     4
     5
     5
     5
     6
     7
     7
     7
     8
     9
     9
    10
     9

I would like this query to run in Oracle.


You can use analytic functions (window functions):

select id, 
   case 
      when lead(id, 1, 0) over (order by id) = id then 'Y'
      else 'N'
   end
  from your_ids_table;

You can replace order by clause with anything you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消