获取上个月的数据以及当月的数据

时间:2018-08-23 13:27:39

标签: sql ms-access access-vba

我有一张大致像这样的表

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

我该如何添加另一列来计算每个特定交易号的当月损益与上个月之差?即

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

2 个答案:

答案 0 :(得分:1)

我认为您可以使用相关的子查询:

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;

计算差异只是算术。

答案 1 :(得分:0)

以表格为基础-并假设每个月在该月的最后一天记录一次交易:

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)

请考虑您有两个表格-一个显示本月的数据,另一个显示上月的数据。
要将两个表连接在一起,您需要将Transaction数字视为两个表之间的公共属性。
两个表是同一个表这一事实与Access无关-只要知道别名是T1T2即可知道哪个是哪个。

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

这将提供太多记录。 7月的交易将在第二个表和上个月合并。 上个月的交易也将加入本月交易。这导致每个事务有四个记录。

| 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           |

要仅对上个月的数字执行计算,我们需要将当前月份加入上个月。

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

此联接中的DATESERIAL计算将日期添加两个月,然后返回第二个月的零天,它等于该月之前的最后一天。 (希望如此)。

| 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 |                     | 

如果必须在开始的月份中有数字,则可以使用NZ将NULL值设为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           |