我有以下代码
SELECT MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value
FROM MeterReadings MR
INNER JOIN MeterReadingDetails MRD ON MRD.ReadingId = MR.sno
INNER JOIN MeterReadingTypes MRT ON MRT.sno = MRD.ReadingTypeId
WHERE MRT.sno IN (7,10,11)
GROUP BY MRT.sno,MRT.TypeName,MR.Adate
ORDER BY MR.Adate DESC
结果
sno TypeName Adate Value
11 Toplam Kapasitif 2013-01-04 00:00:00 33,313
7 Toplam 2013-01-04 00:00:00 7819,33
10 Toplam Reaktif 2013-01-04 00:00:00 640,492
11 Toplam Kapasitif 2013-01-03 00:00:00 33,276
7 Toplam 2013-01-03 00:00:00 7805,934
10 Toplam Reaktif 2013-01-03 00:00:00 639,862
我想要一个名为“OldValue”的附加列。OldValue 列显示前一天的值,如下所示(上例的最后三行)
33,276
7805,934
639,862
null
null
null
我怎样才能做到这一点?可能有一个类似的例子来告诉我方法。
提前致谢...
更新
事实上,我写了这样的东西
SELECT MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value,
(SELECT Value From
(SELECT TOP 1 XMR.Adate,XMRD.ReadingTypeId,MAX(XMRD.Value) AS Value
FROM MeterReadings XMR,MeterReadingDetails XMRD
WHERE XMRD.ReadingId = XMR.sno AND XMRD.ReadingTypeId = MRT.sno AND XMR.Adate<MR.Adate
GROUP BY XMR.Adate,XMRD.ReadingTypeId
ORDER BY XMR.Adate DESC) AS TBL
) AS OldValue
FROM MeterReadings MR
INNER JOIN MeterReadingDetails MRD ON MRD.ReadingId = MR.sno
INNER JOIN MeterReadingTypes MRT ON MRT.sno = MRD.ReadingTypeId
WHERE MRT.sno IN (7,10,11)
GROUP BY MRT.sno,MRT.TypeName,MR.Adate
但我不知道,这是最好的方法吗?