简体   繁体   English

Ms Access 查询前一天的日期

[英]Ms Access query previous day's date

How to bulid a query in Ms Access to include the day before amounts as an opening balance.如何在 Ms Access 中构建查询以将前一天的金额作为期初余额。 So on running the query i enter 3/10/18 in the WorkDay parameter box and records for 3/10/18 and 2/10/18 is shown.因此,在运行查询时,我在 WorkDay 参数框中输入 3/10/18 并显示 3/10/18 和 2/10/18 的记录。 The Table is setup as follows:表设置如下:

WorkDay....TranactionID....Amount WorkDay....TransactionID....Amount

2/10/18......Opening........1000 2/10/18......开业......1000

2/10/18......Credit.........500 2/10/18......信用.......500

2/10/18.......Debit.........300 2/10/18.......借方.......300

3/10/18.......Credit........700 3/10/18.......信用......700

3/10/18.......Debit.........200 3/10/18.......借方.......200

So if I run the query for 3/10/18 it should return因此,如果我运行 3/10/18 的查询,它应该返回

WorkDay....TranactionID....Amount WorkDay....TransactionID....Amount

2/10/18......[Expr].........800 2/10/18......[Expr].......800

3/10/18.......Credit........700 3/10/18.......信用......700

3/10/18.......Debit.........200 3/10/18.......借方.......200

If you are using the GUI add DateAdd("d",-1,[MyDateParameter]) to the OR line under [MyDateParameter] in the Workday field.如果您使用 GUI,请将DateAdd("d",-1,[MyDateParameter])Workday字段中[MyDateParameter]下的 OR 行。

For SQL WHERE statement you would use对于 SQL WHERE语句,您将使用

WorkDay=[MyDateParameter] OR Workday=DateAdd("d",-1,[MyDateParameter])

Obviously substitute [MyDateParameter] with whatever your date parameter actually is.显然,用您的日期参数实际替换[MyDateParameter]

First some notes about the request:首先是关于请求的一些说明:

  1. The desired results imposes different requirements for the current day vs the previous day, so there must be two different queries.所需的结果对当天和前一天提出了不同的要求,因此必须有两个不同的查询。 If you want them in one result set, you would need to use a UNION.如果您希望它们在一个结果集中,则需要使用 UNION。

    • (You could write a single SQL UNION query, but since UNION queries do not work at all with the visual designer, you are left to write and test the query without any advantages of the query Design View. My preference is therefore to create two saved queries instead of embedded subqueries, then create a UNION which combines the results of the saved queries.) (您可以编写单个 SQL UNION 查询,但由于 UNION 查询根本不适用于可视化设计器,因此您只能编写和测试查询,而没有查询设计视图的任何优势。因此,我的偏好是创建两个已保存的查询而不是嵌入的子查询,然后创建一个联合保存的查询的结果。)
  2. Neither the question, nor answers to comments indicate what to do with any exceptions, like missing dates, weekends, etc. The following queries take the "day before" literally without exception.问题和评论的答案都没有表明如何处理任何例外情况,例如缺少日期、周末等。以下查询从字面上看无一例外地采用“前一天”。

  3. The other difficulty is that the Credit entries also have a positive amount, so you must handle them specially.另一个困难是 Credit 条目也有正数,因此您必须专门处理它们。 If Credits were saved with negative values, the summation would be simple and direct.如果 Credits 以负值保存,则求和将简单而直接。

QueryCurrent:查询当前:

PARAMETERS [Which WorkDay] DateTime;
SELECT S.WorkDay, S.TransactionID, Sum(S.[Amount]) As Amount
FROM [SomeUnspecifiedTable] As S
WHERE S.WorkDay = [Which WorkDay]
GROUP BY S.WorkDay, S.TransactionID

QueryPrevious:查询上一个:

PARAMETERS [Which WorkDay] DateTime;
SELECT S.WorkDay, "[Expr]" As TransactionID, 
  Sum(IIF(S.TransactionID = "Credit", -1, 1) * S.[Amount]) As Amount
FROM [SomeUnspecifiedTable] As S
WHERE S.WorkDay = ([Which WorkDay] - 1)
GROUP BY S.WorkDay

Union query:联合查询:

SELECT * FROM QueryCurrent
UNION
SELECT * FROM QueryPrevious
ORDER BY [WorkDay]

Notes about the solution:关于解决方案的注意事项:

  • You could also use DateAdd() function, but add/subtracting integers from dates defaults to a change of days.您也可以使用 DateAdd() 函数,但从日期中添加/减去整数默认为更改天数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM