我正在使用Access 2010表,其中包含日期,单位ID,抄表以及更正。我想通过获取当前日期并用之前的日期减去每日读取电表。以下是我的表格示例:
InputDate | UnitID | MeterReading | Correction
--------- | ------ | ------------ | ----------
11/1/2016 | U1 | 41476 | 10000
11/1/2016 | U2 | 5991 | 10000
11/1/2016 | U3 | 164205 | 1000000
11/1/2016 | U4 | 11160 | 100000
11/2/2016 | U1 | 41490 | 10000
11/2/2016 | U2 | 5991 | 10000
11/2/2016 | U3 | 164205 | 1000000
11/2/2016 | U4 | 11169.3 | 100000
我尝试过多种不同的方式。我让它工作的唯一方法是进行查询以拆分每个单元的表(例如U1,U2等)。从那里我将查询该结果并进行此计算:
(
([MeterReading]+[Correction]) -
(DLookUp("MeterReading", "tblA", "[tblA].[InputDate] = #" &
DateAdd("d",-1,[tblA].[InputDate]) & "#")+[Correction])
)
然后我会再做一个查询,按日期对所有数据进行分组。
我的问题是,是否有更直观的方法来做到这一点?我已经研究了尝试进行SQL查询,嵌套查询以及按Unit_ID进行分组的方法,但我的计算结束了。我认为DLookUp在进行计算时没有查看正确的单位,因为同一天有多个单位。此外,当我尝试按InputDate和UnitID分组时,我得到“您的查询不包括指定的表达式”。我是SQL语言的新手,所以任何帮助都会受到赞赏。
示例输出将是:
InputDate | UnitID | MeterRunTime
--------- | ------ | ------------
11/2/2016 | U1 | 14
11/2/2016 | U2 | 0
11/2/2016 | U3 | 0
11/2/2016 | U4 | 9.3
答案 0 :(得分:0)
如果我理解正确,以下内容应该能满足您的需求。
SELECT tblA.InputDate, tblA.UnitID,
([tblA].[MeterReading]+[tblA].[Correction])-
([tblA_1].[MeterReading]+[tblA_1].[Correction]) AS MeterRunTime
FROM tblA INNER JOIN tblA AS tblA_1
ON (tblA.InputDate-1 = tblA_1.InputDate) AND (tblA.UnitID = tblA_1.UnitID);
答案 1 :(得分:0)
注意:我对SQL有点生疏,所以毫无疑问有人可以改进这一点。
要返回下一个可用日期,您需要找到小于每个UnitID的当前日期的最大日期,以及相同的当前日期。
这两个SQL语句将执行此操作:
SELECT MAX(tblA_1.InputDate) AS MaxDate
,tblA_1.UnitID
FROM tblA tblA_1 INNER JOIN tblA tblA_2 ON tblA_1.UnitID = tblA_2.UnitID AND
tblA_1.InputDate < tblA_2.InputDate
GROUP BY tblA_1.UnitID
SELECT MAX(InputDate) AS CurrentDate
,UnitID
FROM tblA
GROUP BY UnitID
接下来,您需要将Meter Reading + Correction包含在之前的结果中:
SELECT MaxDate
,T1.UnitID
,MeterReading+Correction AS LastReading
FROM tblA T1 INNER JOIN
(
SELECT MAX(tblA_1.InputDate) AS MaxDate
,tblA_1.UnitID
FROM tblA tblA_1 INNER JOIN tblA tblA_2 ON tblA_1.UnitID = tblA_2.UnitID AND
tblA_1.InputDate < tblA_2.InputDate
GROUP BY tblA_1.UnitID
) T2 ON T1.InputDate = T2.MaxDate
GROUP BY MaxDate
,T1.UnitID
,MeterReading+Correction
SELECT CurrentDate
,T4.UnitID
,MeterReading+Correction AS CurrentReading
FROM tblA T3 INNER JOIN
(
SELECT MAX(InputDate) AS CurrentDate
,UnitID
FROM tblA
GROUP BY UnitID
) T4 ON T3.InputDate = T4.CurrentDate AND T3.UnitID = T4.UnitID
GROUP BY CurrentDate
,T4.UnitID
,MeterReading+Correction
最后,你可以把所有的东西粘在一起,以得出你的最终结果:
SELECT CurrentDate
,MaxDate
,T1.UnitID
,LastReading
,CurrentReading
,CurrentReading - LastReading AS FinalReading
FROM
(
SELECT MaxDate
,T1.UnitID
,MeterReading+Correction AS LastReading
FROM tblA T1 INNER JOIN
(
SELECT MAX(tblA_1.InputDate) AS MaxDate
,tblA_1.UnitID
FROM tblA tblA_1 INNER JOIN tblA tblA_2 ON tblA_1.UnitID = tblA_2.UnitID AND
tblA_1.InputDate < tblA_2.InputDate
GROUP BY tblA_1.UnitID
) T2 ON T1.InputDate = T2.MaxDate
GROUP BY MaxDate
,T1.UnitID
,MeterReading+Correction
) T5
INNER JOIN
(
SELECT CurrentDate
,T4.UnitID
,MeterReading+Correction AS CurrentReading
FROM tblA T3 INNER JOIN
(
SELECT MAX(InputDate) AS CurrentDate
,UnitID
FROM tblA
GROUP BY UnitID
) T4 ON T3.InputDate = T4.CurrentDate AND T3.UnitID = T4.UnitID
GROUP BY CurrentDate
,T4.UnitID
,MeterReading+Correction
) T6 ON T5.UnitID = T6.UnitID