Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 12890

Diffrence of last 2 rows

$
0
0

Hi i have a table which includes meter reading of different meters. When i give the meter id i need to take the last 2 meter reading values . My table structure and datas are

CREATE TABLE MeterReading ( ReadingID int, MeterID int, DataCounter int, Value numeric(16,4) )

INSERT INTO MeterReading 
VALUES(1, 1, 1, 102.45)
INSERT INTO MeterReading 
VALUES(2, 2, 1, 110.45)
INSERT INTO MeterReading 
VALUES(3, 1, 2, 110.58)
INSERT INTO MeterReading 
VALUES(4, 2, 2, 115.89)
INSERT INTO MeterReading 
VALUES(5, 1, 4, 120.00)
INSERT INTO MeterReading 
VALUES(6, 2, 4, 116.23)

Here i need the difference of latest (based on DataCounter) difference of Values corresponding MeterID

select top 2 * from MeterReading  
where MeterID = 1
order by DataCounter desc
I give the select query. so that i need to take the diffrence of these selected rows' Values. Always the latest value is larger




Viewing all articles
Browse latest Browse all 12890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>