I have a table with a value
column. I want to calculate the last row minus the first row, as shown here:
id value
1 10
2 45
3 65
4 95
. .
. .
. .
500 200
I want to obtain 200 - 10 = 190
I've tried to use the below command in SQL Server 2012, however LAST
and FIRST
don't work.
SELECT LAST(Value) - FIRST(Value) FROM Counter;
What is the syntax for this command in SQL Server?
Best Answer
You were close -
FIRST
andLAST
are from Access; in SQL Server (starting with SQL Server 2012) they areFIRST_VALUE()
andLAST_VALUE()
.So, if you are 2012 or better (or Azure SQL Database), here's one way to get your answer: