查看上一条和下一条记录

时间:2019-01-15 11:55:22

标签: tsql lag lead

我正在尝试比较不同时期的不同费用。但是我不知道如何将单个记录与之前和之后的记录进行比较。当一条记录的成本与之前的记录和之后的记录相同时,我需要的是数据集中的是或否。

我的数据集如下:

+--------+-----------+----------+------------+-------+-----------+
| Client | Provision | CAK Year | CAK Period | Costs | Serial Nr |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    210    |   2017   |     13     |  150  |     1     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    210    |   2018   |     1      |  200  |     2     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    210    |   2018   |     2      |  170  |     3     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    210    |   2018   |     3      |  150  |     4     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    210    |   2018   |     4      |  150  |     5     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    210    |   2018   |     5      |  150  |     6     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    689    |   2018   |     1      |  345  |     1     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    689    |   2018   |     2      |  345  |     1     |
+--------+-----------+----------+------------+-------+-----------+
|   1    |    689    |   2018   |     3      |  345  |     1     |
+--------+-----------+----------+------------+-------+-----------+

到目前为止我已经尝试过:

CASE
WHEN Provision = Provision
AND Costs = LEAD(Costs, 1, 0) OVER(ORDER BY CAK Year, CAK Period)
AND Costs = LAG(Costs, 1, 0) OVER(ORDER BY CAK Year, CAK Period)
THEN 'Yes
ELSE 'No'
END

我的预期结果:

+--------+-----------+----------+------------+-------+-----------+--------+
| Client | Provision | CAK Year | CAK Period | Costs | Serial Nr | Result |
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    210    |   2017   |     13     |  150  |     1     |   No
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    210    |   2018   |     1      |  200  |     2     |   No
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    210    |   2018   |     2      |  170  |     3     |   No
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    210    |   2018   |     3      |  150  |     4     |   No
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    210    |   2018   |     4      |  150  |     5     |  Yes
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    210    |   2018   |     5      |  150  |     6     |   No
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    689    |   2018   |     1      |  345  |     1     |   No
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    689    |   2018   |     2      |  345  |     1     |  Yes
+--------+-----------+----------+------------+-------+-----------+--------+
|   1    |    689    |   2018   |     3      |  345  |     1     |   No
+--------+-----------+----------+------------+-------+-----------+--------+

由于我没有得到预期的结果,你们可以进一步帮助我吗?

1 个答案:

答案 0 :(得分:1)

您需要添加partition by Provision,否则您的laglead排序将在所有Provision值上运行:

declare @d table(Client int,Provision int,CAKYear int, CAKPeriod int, Costs int, SerialNr int);
insert into @d values
 (1,210,2017,13,150,1)
,(1,210,2018,1,200,2)
,(1,210,2018,2,170,3)
,(1,210,2018,3,150,4)
,(1,210,2018,4,150,5)
,(1,210,2018,5,150,6)
,(1,689,2018,1,345,1)
,(1,689,2018,2,345,1)
,(1,689,2018,3,345,1);

select *
        ,case when Provision = Provision
                and Costs = lead(Costs, 1, 0) over(partition by Provision order by CAKYear, CAKPeriod)
                and Costs = lag(Costs, 1, 0) over(partition by Provision order by CAKYear, CAKPeriod)
            then 'Yes'
            else 'No'
            end as Result
from @d
order by Provision
        ,CAKYear
        ,CAKPeriod;

输出

+--------+-----------+---------+-----------+-------+----------+--------+
| Client | Provision | CAKYear | CAKPeriod | Costs | SerialNr | Result |
+--------+-----------+---------+-----------+-------+----------+--------+
|      1 |       210 |    2017 |        13 |   150 |        1 | No     |
|      1 |       210 |    2018 |         1 |   200 |        2 | No     |
|      1 |       210 |    2018 |         2 |   170 |        3 | No     |
|      1 |       210 |    2018 |         3 |   150 |        4 | No     |
|      1 |       210 |    2018 |         4 |   150 |        5 | Yes    |
|      1 |       210 |    2018 |         5 |   150 |        6 | No     |
|      1 |       689 |    2018 |         1 |   345 |        1 | No     |
|      1 |       689 |    2018 |         2 |   345 |        1 | Yes    |
|      1 |       689 |    2018 |         3 |   345 |        1 | No     |
+--------+-----------+---------+-----------+-------+----------+--------+