简体   繁体   中英

Getting previous month data

I have a transaction records table in ms-access with item field, date field and quantity field. I would like to add a new column, with transaction quantity for the previous month. For example, I have a row that looks like this:

+----------------------------+
| Item A | 1/11/2018 | 8,000 |
+----------------------------+
| Item A | 1/12/2018 | 7,000 |
+----------------------------+

And I want to add in a new column that shows previous month's transaction quantity, which will look like this:

+------------------------------------+
| Item A | 1/11/2018 | 8,000 | -     |
+------------------------------------+
| Item A | 1/12/2018 | 7,000 | 8,000 |
+------------------------------------+

Can anyone suggest how do I query this in access?

In MS Access, you can do something like this. MS Access does not have LAG function , as similar to T-SQL.

SELECT ItemA, Date, Quantity as currentQuantity, 
(SELECT top 1 Quantity FROM Table1 new WHERE new.Date = dateadd("m",-1,tbl.Date)) as previousQuantity
FROM Table1 tbl 
ORDER BY tbl.Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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