我有一个Access .mdb数据库,其表格看起来类似于:
+---------+------+--------+
| date | value |
+---------+------+--------+
|2011-05-04 12:00 | 45.9 |
|2011-05-05 12:00 | 21.2 |
|2011-05-06 12:00 | 32.2 |
|2011-05-07 12:00 | 30.4 |
|2011-05-08 12:00 | 40.4 |
|2011-05-09 12:00 | 19.8 |
|2011-05-10 12:00 | 29.7 |
+-------+---------+-------+
我想创建一个查询,该查询将返回从前一天的值中减去一个值而得到的值。
例如:查询将计算 (21.2-45.9)并返回-24.7 (32.2-21.2)并返回-11.0 (30.4-32.2)并返回-1.8 等
如何在SELECT语句中完成此操作?
答案 0 :(得分:2)
您可以使用在相关表格中使用自我加入的查询:
SELECT
t.dateValue , t.singleValue - IIF(ISNULL(tNext.singleValue), 0, tNext.singleValue)
FROM
test t
LEFT JOIN test tNext
ON t.dateValue = DateAdd("d", -1, tNext.dateValue)
WHERE
t.dateValue = #2011-05-08 12:00#;
输出
dateValue Expr1001
---------------------- ----------------
05/08/2011 12:00:00 PM 20.6000022888184
DDL并在
下面插入CREATE TABLE test (dateValue DATETIME, singleValue SINGLE);
INSERT INTO test VALUES (#2011-05-04 12:00#, 45.9);
INSERT INTO test VALUES (#2011-05-05 12:00#, 21.2);
INSERT INTO test VALUES (#2011-05-06 12:00#, 32.2);
INSERT INTO test VALUES (#2011-05-07 12:00#, 30.4);
INSERT INTO test VALUES (#2011-05-08 12:00#, 40.4);
INSERT INTO test VALUES (#2011-05-09 12:00#, 19.8);
INSERT INTO test VALUES (#2011-05-10 12:00#, 29.7);
答案 1 :(得分:0)
以下是适用于我的查询:
SELECT t2.date, t2.value-t1.value
FROM Table1 AS t1, Table1 AS t2
WHERE t2.date=DATEADD("d",1,t1.date);
再次感谢Tom H。