Reputation: 2010
I am puzzled over LEAD returning always ID=1, which is not even an ID in the table (ID start at ~18k), instead of a valid ID for the next record. NULL values are where they supposed to be, it's just the rows, which supposed to contain a valid ID. The LAG with the same syntax works as expected, returning correct values. The LEAD doesn't even work (correctly), when I comment out LAG. By the way, I copied the code (and just changed table and column names) from my other script, where it was working fine.
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
Here is a sample of results using SELECT (see the 1 values in the last column):
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
I fear it might be some dumb typo mistake I am blind to see. Or are there any catches with LAG and LEAD?
Upvotes: 2
Views: 1975
Reputation: 1270443
First, you can write this much more simply as:
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;
I cannot explain the behavior that you see. Two things to check:
Upvotes: 3
Reputation: 35603
Unable to replicate you issue, see this SQL Fiddle
MS SQL Server 2014 Schema Setup:
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)
;
Query 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
| 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) |
Upvotes: 1