santosh kumar
santosh kumar

Reputation: 11

compare current row value with next row value

    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                 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.

Upvotes: 1

Views: 466

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

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.

Upvotes: 2

Related Questions