How to use lead or Lag for a Group in oracle or any other analytic function to get desired result?

Random Guy

input

id   name     date   value
1     aa      x      v1
1     aa      y      v1
1     aa      z      v1
2     bb      a      v2
2     bb      b      v2
3     cc      c      v2
4     dd      d      v3
4     dd      e      v3
5     ee      f      v4

output

   id   name     date   value  lead value
    1     aa      x      v1      v2
    1     aa      y      v1      v2
    1     aa      z      v1      v2
    2     bb      a      v2      v2
    2     bb      b      v2      v2
    3     cc      c      v2      v3
    4     dd      d      v3      v4
    4     dd      e      v3      v4
    5     ee      f      v4     null

How to get lead value for a id? i.e for id 1 lead value is v2,for id 2 lead value again is v2(as id 3 value is v2), for id 3 lead value is v3 and so on..

Atif

You can use the below query to get the desired result:

with 
CTE1(ID, VALUE) AS
(select distinct id, 
 value from table1 
 order by id
),
CTE2 AS
(
SELECT A.ID, 
  A.VALUE, 
  LEAD(A.VALUE) OVER(ORDER BY A.ID) LEAD_VALUE 
FROM CTE1 A
)
SELECT A.*, B.LEAD_VALUE 
FROM TABLE1 A, CTE2 B
WHERE A.ID = B.ID;

DB_FIDDLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL mimicking analytic LEAD/LAG function with some restrictions

how to use spark lag and lead over group by and order by

Lead function group by in oracle

How to use LEAD and LAG In Where?

How do I use lag function to get my desired df from my source df?

how to use lag/lead function in spark streaming application?

how to use a variable name within dplyr::lead/lag function

SQL: Use Lag/Lead result twice in the select

How to use Lead() and Lag() to get the first non null value

oracle lag function with group by

Pandas equivalent of Oracle Lead/Lag function

Optimize self-join Oracle SQL query with LAG/LEAD analytic functions?

MySQL lag/lead function?

R lead lag function summarize within group and calculate percent

How to ignore nulls in an Oracle analytic function

How to use group by function in oracle

How to get only one result with postgres lag() function

How to use DurationFormatUtils class to get the result in desired format?

How to use an Inner Join to get desired result in SQL Server 2008

PHP and mysql how to use joins to get the desired result

Is this a bug in group_by and lead/lag?

r lag and lead indicator by group

Oracle: Combine two group by queries (which use aggregate function count()) by union or so to get a consolidated result

lead or lag function to get several values, not just the nth

MySql: how to get the desired result

how to get the desired result of inheritance?

How can i use "strcspn" or "any other Approach to change key of Array" to get an appropriate result?

How to use lag/lead in mutate with only one initial value?

How to use LAG & LEAD window functions for different groups of observations