What is the difference between LEAD and LAG?

Hi all
Please let me know the difference between LEAD and LAG even if both are functions. Analytics, where we can use specific to improve performance.

Thanks in advance.

concerning
PRAMOD

Pramod salvation,

pramod62917 wrote:
Please let me know the difference between LEAD and LAG

The main difference is in which way is 'research ':
-Lead will "transfer" in respect of the order by clause
-Lag goes "back" with regard to the order by clause

See this example (Basic):

Scott@my11g SQL>ed
Wrote file afiedt.buf

  1  with t as (
  2  select 'TeamX' team, 1 player_id, 'PlayerA' name from dual
  3  union all select 'TeamX' team, 2 player_id, 'PlayerB' name from dual
  4  union all select 'TeamX' team, 3 player_id, 'PlayerC' name from dual
  5  union all select 'TeamX' team, 4 player_id, 'PlayerD' name from dual
  6  union all select 'TeamZ' team, 1 player_id, 'PlayerJ' name from dual
  7  union all select 'TeamZ' team, 2 player_id, 'PlayerK' name from dual
  8  union all select 'TeamZ' team, 3 player_id, 'PlayerL' name from dual
  9  -- end of sample data --
 10  )
 11  select
 12  team
 13  ,player_id current_play_id
 14  ,name current_name
 15  ,lead(name) over (partition by team order by player_id) next_name
 16  ,lag(name) over (partition by team order by player_id) prev_name
 17* from t
Scott@my11g SQL>/

TEAM  CURRENT_PLAY_ID CURRENT NEXT_NA PREV_NA
----- --------------- ------- ------- -------
TeamX               1 PlayerA PlayerB
TeamX               2 PlayerB PlayerC PlayerA
TeamX               3 PlayerC PlayerD PlayerB
TeamX               4 PlayerD         PlayerC
TeamZ               1 PlayerJ PlayerK
TeamZ               2 PlayerK PlayerL PlayerJ
TeamZ               3 PlayerL         PlayerK

7 rows selected.

On the first line + (team = TeamX, current_play_id = 1) +, the next_name once PlayerA regarding the order given by +(order by player_id) + team +(partition by team) + playerB. For this line it is not prev_name like no player as an inferior player_id wihtin this team.
On the 3rd line +(team=TeamX, current_play=3) +, the once next_name PlayerC in regards to the order given by the team PlayerD, the previous is PlayerB.

Tags: Database

Similar Questions

Maybe you are looking for

  • Satellite Pro S500-139 - FN + F9 key does not have to turn off the touchpad

    Hello I use a Satellite Pro S500-139 and am having problems with my F9 key. When I tap my palm touches the mouse and send the cursor somewhere else on the page. I reinstalled the ALPS pointing device and now have access to the Advanced setting that I

  • Why should I pay for applications that were free?

    MacBook Pro, end of 2015. I downloaded applications iWork - Pages, Numbers, Keynote, free. I had to format my mac, and now when I go to download, they are $20 each. Can I please get my apps back? I seriously need them and I don't have $60. I have fou

  • HP: Laptop randomly stops when it hits 15%

    He started only today. My laptop would randomly just turned off when the battery was low. When I checked the percentage it shows 15% when I turn on my laptop. I already checked power options, and everything is normal. I was wondering if anyone has so

  • Re: Satellite L305 - help upgrade memory RAM

    I had a Vista 32 bit SP1 L305. It came with 2 GB memory DDR2 6400 (two sticks of 1 GB). I have upgraded to 4 GB using two sticks of 2B) is there something else I should do to get the win to use additional memory. I've seen many debates about 32-bit p

  • NIPXISM port conflict

    NIPXISM. EXE seems to grasp from a number of ports and sometimes catches a port that I need for other purposes.  I can say that he not enter a particular port or start typing another series?