sqlplus - How to calculate difference between two rows in SQL? -


my tables are:

bowlers: bname - handed - phone

performances: bname - tname - score

tournaments: tname - tdate

i trying figure out how calculate difference in score bowler named fred. code far is:

select tname, tdate, score tournaments natural inner join  performances bname = 'fred' order tdate; 

this gives me table this:

 tname                    tdate                score  ------------------------ ------------ -------------  tournament 1             1/1/2014               250  tournament 2             1/8/2014               245  tournament 3             2/10/2014              215 

now need add fourth column calculates difference in score previous tournament. finished table this:

 tname                    tdate                score     score_dif  ------------------------ ------------ ------------- -------------  tournament 1             1/1/2014               250             0  tournament 2             1/8/2014               245            -5  tournament 3             2/10/2014              215           -30 

any appreciated!

sqlplus attached oracle, supports lag() function. so, can want pretty easily:

select tname, tdate, score,        coalesce(score - lag(score) on (order tdate), 0) diff tournaments natural inner join       performances bname = 'fred' order tdate; 

Comments