在最近的记录之前选择记录

时间:2018-10-12 02:44:32

标签: sql sqlite

我想在表中某项的最后一条记录之前获得上一条记录

SELECT MAX(BillID), ConsumerID_FK,
SELECT CurrentReading FROM tblReading ORDER BY ReadingID DESC LIMIT 1,1) AS 
'PreviousReading',
CurrentReading 
FROM tblBilling b INNER JOIN tblReading r ON r.ReadingID = b.ReadingID_FK 
INNER JOIN tblconsumer c ON c.ConsumerID = r.ConsumerID_FK
GROUP BY r.ReadingID

enter image description here

仅返回ConsumerID_FK=28的上一条记录。以前的11读数是21。我想为所有消费者做到这一点。

我希望它返回每个消费者的先前读数。

Max(billID)  | ConsumerID_FK  | Previous Reading | CurrentReading
------------------------------------------------------------------
     1       |     11         |      21          |      32  
     2       |     28         |      12          |      16                                 

0 个答案:

没有答案