LAG 有效,LEAD 在同一语句中返回 1

LAG works, LEAD returning 1 within the same statement

我对 LEAD 总是返回 ID=1 感到困惑,它甚至不是 table 中的 ID(ID 从 ~18k 开始),而不是下一条记录的有效 ID。 NULL 值是它们应该在的位置,它只是应该包含有效 ID 的行。具有相同语法的 LAG 按预期工作,返回正确的值。当我注释掉 LAG 时,LEAD 甚至无法(正确)工作。 顺便说一下,我从我的其他脚本中复制了代码(并且只是更改了 table 和列名),它运行良好。

UPDATE PRJ SET 
    PrevJob = SRC.PrevID,  -- << write Previous ID
    NextJob = SRC.NextID   -- << write Next ID
FROM PRJ as PRJ
LEFT JOIN  (
      SELECT 
          ID,  -- << ID for joining to the original record
          LAG(ID)  OVER (PARTITION BY RPCode,PNInt ORDER BY OrderNo) AS PrevID, -- << previous works
          LEAD(ID) OVER (PARTITION BY RPCode,PNInt ORDER BY OrderNo) AS NextID  -- << next returns 1
      FROM PRJ as PRJ2 
      ) as SRC ON SRC.ID = PRJ.ID

这是使用 SELECT 的结果示例(请参阅最后一列中的 1 个值):

ID       PNInt       RPCode               OrderNo PrevJob              NextJob
-------- ----------- -------------------- ------- -------------------- -------
18783    53          00005320171113120000 1       NULL                 1
18795    53          00005320171113120000 2       18783                1
18789    53          00005320171113120000 3       18795                NULL
18784    53          00005320171127120000 1       NULL                 1
18796    53          00005320171127120000 2       18784                1
18790    53          00005320171127120000 3       18796                NULL
18785    53          00005320171211120000 1       NULL                 1
18797    53          00005320171211120000 2       18785                1
18791    53          00005320171211120000 3       18797                NULL
18786    53          00005320171225120000 1       NULL                 1
18798    53          00005320171225120000 2       18786                1
18792    53          00005320171225120000 3       18798                NULL
18787    53          00005320180108120000 1       NULL                 1
18799    53          00005320180108120000 2       18787                1
18793    53          00005320180108120000 3       18799                NULL

我担心这可能是我看不到的愚蠢的拼写错误。或者 LAG 和 LEAD 有什么问题吗?

无法复制您的问题,请参阅 this SQL Fiddle

MS SQL Server 2014 架构设置:

CREATE TABLE PRJ
    ([ID] int, [PNInt] int, [RPCode] varchar(20), [OrderNo] int, [PrevJ] varchar(5), [NextJ] varchar(4))
;

INSERT INTO PRJ
    ([ID], [PNInt], [RPCode], [OrderNo], [PrevJ], [NextJ])
VALUES
    (18783, 53, 'O0005320171113120000', 1, NULL, '1'),
    (18795, 53, 'O0005320171113120000', 2, '18783', '1'),
    (18789, 53, 'O0005320171113120000', 3, '18795', NULL),
    (18784, 53, 'O0005320171127120000', 1, NULL, '1'),
    (18796, 53, 'O0005320171127120000', 2, '18784', '1'),
    (18790, 53, 'O0005320171127120000', 3, '18796', NULL),
    (18785, 53, 'O0005320171211120000', 1, NULL, '1'),
    (18797, 53, 'O0005320171211120000', 2, '18785', '1'),
    (18791, 53, 'O0005320171211120000', 3, '18797', NULL),
    (18786, 53, 'O0005320171225120000', 1, NULL, '1'),
    (18798, 53, 'O0005320171225120000', 2, '18786', '1'),
    (18792, 53, 'O0005320171225120000', 3, '18798', NULL),
    (18787, 53, 'O0005320180108120000', 1, NULL, '1'),
    (18799, 53, 'O0005320180108120000', 2, '18787', '1'),
    (18793, 53, 'O0005320180108120000', 3, '18799', NULL)
;

查询 1:

      SELECT 
          ID,  -- << ID for joining to the original record
          LAG(ID)  OVER (PARTITION BY RPCode,PNInt ORDER BY OrderNo) AS PrevID, -- << previous works
          LEAD(ID) OVER (PARTITION BY RPCode,PNInt ORDER BY OrderNo) AS NextID  -- << next returns 1
      FROM PRJ as PRJ2 

Results:

|    ID | PrevID | NextID |
|-------|--------|--------|
| 18783 | (null) |  18795 |
| 18795 |  18783 |  18789 |
| 18789 |  18795 | (null) |
| 18784 | (null) |  18796 |
| 18796 |  18784 |  18790 |
| 18790 |  18796 | (null) |
| 18785 | (null) |  18797 |
| 18797 |  18785 |  18791 |
| 18791 |  18797 | (null) |
| 18786 | (null) |  18798 |
| 18798 |  18786 |  18792 |
| 18792 |  18798 | (null) |
| 18787 | (null) |  18799 |
| 18799 |  18787 |  18793 |
| 18793 |  18799 | (null) |

首先,你可以更简单地写成:

UPDATE toupdate
    SET PrevJob = SRC.PrevID,  -- << write Previous ID
        NextJob = SRC.NextID   -- << write Next ID
FROM (SELECT PRJ.*, 
             LAG(ID)  OVER (PARTITION BY RPCode, PNInt ORDER BY OrderNo) AS new_PrevID, -- << previous works
             LEAD(ID) OVER (PARTITION BY RPCode, PNInt ORDER BY OrderNo) AS new_NextID  -- << next returns 1
      FROM PRJ
     ) toupdate;

我无法解释您看到的行为。要检查两件事:

  • id 字段的类型。
  • 可能在 table 上定义的任何触发器。