Get previous month's data alongside current month

风格不统一 提交于 2019-12-14 03:05:25

问题


I have a table that roughly looks like this

Transaction   Date        Gain/Loss
15464         7/31/2018    $500
15464         6/30/2018   -$200
59872         7/31/2018    $1000
59872         6/30/2018    $2500

How can i add another column that calculates the difference between the current month's gain/loss and the previous month's for each specific transaction number? i.e.

Transaction   Date        Gain/Loss    Change in Gain/Loss
15464         7/31/2018    $500         $700
15464         6/30/2018   -$200        -$200
59872         7/31/2018    $1000       -$1500
59872         6/30/2018    $2500        $2500

回答1:


I think you can use a correlated subquery:

select t.*,
       (select top (1)
        from t as t2
        where t2.transaction = t.transaction and t2.date < t.date
        order by t2.date desc
       ) as prev_gain_loss
from t;

Calculating the difference is just arithmetic.




回答2:


Using your table as a base - and going on the assumption that each month records a single transaction on the last day of the month:

SELECT  T1.Transaction
        , T1.[Date]
        , T1.[Gain/Loss]
        , T2.[Date]
        , T1.[Gain/Loss]-T2.[Gain/Loss] AS [Change in Gain/Loss]
FROM    MyTable T1 LEFT JOIN MyTable T2 ON T1.Transaction = T2.Transaction AND
                                           T1.[Date] = DATESERIAL(YEAR(T2.[Date]), MONTH(T2.[Date])+2,0)

Consider that you have two tables - one displaying the figures for this month, and one displaying the figures for last month.
To join the two tables together you'd look at the Transaction number as the common attribute between the two tables.
The fact that both tables are the same table is irrelevant to Access - as long as it knows which is which by aliasing the table names as T1 and T2.

FROM MyTable T1 LEFT JOIN MyTable T2 ON T1.Transaction = T2.Transaction

This would give too many records though. The transaction for July would join to itself in the second table, and to the previous month. The previous month transaction would also join to itself and the current month transaction. This results in four records per transaction.

| Transaction | T1.Date    | Gain/Loss | T2.Date    | Change in Gain/Loss |
|-------------|------------|-----------|------------|---------------------|
| 15464       | 31/07/2018 | £500.00   | 30/06/2018 | £700.00             |
| 15464       | 31/07/2018 | £500.00   | 31/07/2018 | £0.00               |
| 15464       | 30/06/2018 | -£200.00  | 30/06/2018 | £0.00               |
| 15464       | 30/06/2018 | -£200.00  | 31/07/2018 | -£700.00            |
| 59872       | 31/07/2018 | £1,000.00 | 30/06/2018 | -£1,500.00          |
| 59872       | 31/07/2018 | £1,000.00 | 31/07/2018 | £0.00               |
| 59872       | 30/06/2018 | £2,500.00 | 30/06/2018 | £0.00               |
| 59872       | 30/06/2018 | £2,500.00 | 31/07/2018 | £1,500.00           |

To only perform the calculation on the previous month figure we need to join the current month to the previous month.

T1.[Date] = DATESERIAL(YEAR(T2.[Date]), MONTH(T2.[Date])+2,0)

The DATESERIAL calculation in this join adds two months to the date, it then returns day zero of the second month which is equivalent to the last day of the month before that. (Hope that made sense).

| Transaction | Date       | Gain/Loss | Change in Gain/Loss |
|-------------|------------|-----------|---------------------|
| 15464       | 31/07/2018 | £500.00   | £700.00             |
| 15464       | 30/06/2018 | -£200.00  |                     |
| 59872       | 31/07/2018 | £1,000.00 | -£1,500.00          |
| 59872       | 30/06/2018 | £2,500.00 |                     | 

If you must have figures in the starting month then you can use NZ to turn the NULL values to 0.

, T1.[Gain/Loss]-NZ(T2.[Gain/Loss],0) AS [Change in Gain/Loss]

| Transaction | Date       | Gain/Loss | Change in Gain/Loss |
|-------------|------------|-----------|---------------------|
| 15464       | 31/07/2018 | £500.00   | £700.00             |
| 15464       | 30/06/2018 | -£200.00  | -£200.00            |
| 59872       | 31/07/2018 | £1,000.00 | -£1,500.00          |
| 59872       | 30/06/2018 | £2,500.00 | £2,500.00           |


来源:https://stackoverflow.com/questions/51986917/get-previous-months-data-alongside-current-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!