LAG工作,LEAD在同一语句中返回1

时间:2017-11-20 10:27:26

标签: sql sql-server lead

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

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是否有任何捕获?

2 个答案:

答案 0 :(得分:2)

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

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字段的类型。
  • 可能在桌面上定义的任何触发器。

答案 1 :(得分:1)

无法复制您的问题,请参阅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) |