Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing and adding row data

Status
Not open for further replies.

Deepa06

Programmer
Nov 27, 2020
1
0
0
GB
I have to compare 'MOVINGAVERAGE' column of current row with next row until 'MOVINGAVERAGE' column of next row has greater value than previous column.
 
Depending on your version of SQL, Lead and Lag will allow you to look a next and previous row values. Otherwise you have to set up a Cursor (I hope not).
 
Depending on your version of SQL ....

I believe both Lead and Lag were introduced in SQL Server 2012. Other implementations (such as MySQL and Oracle) also support these functions.

They are really useful, because SQL does not normally have the concept of "next row" or "previous row" (unlike Visual FoxPro, where there is the concept of a record pointer). Without these functions, you could probably do it with some sort of self-join: first, getting rows where the relevant value is greater than that of the base row; then getting the first (or last) row in that result set, in a given order. But I won't try to give an example.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top