LEAD/LAG 不是 Incrementing/Decrementing
LEAD/LAG Not Incrementing/Decrementing
我无法将 Lead/Lag 函数正确地 increment/decrement 和 return 正确的值。无论我做什么,它仍然是 return 当前行。
当 p.field3 值 = '223344' 时,我需要显示先前的 p.field2 值和后续的 p.field2 值。
BEGIN TRY
SELECT p.field1,
p.field2,
LEAD( p.field2, 1, 'No Lead') OVER (ORDER BY p.field1) AS 'Lead',
LAG( p.field2, 1, 'No Lag' ) OVER (ORDER BY p.field1) AS 'Lag',
p.field3,
p.field4
FROM fieldTable p
WHERE p.field3 = '223344'
ORDER BY p.field1
END TRY
BEGIN CATCH
.....
END CATCH
Example Data in Database
1, 'Mr. Smith', '112233', 'Info1'
2, 'Mr. Jones', '223344', 'Info2'
3, 'Mr. Davis', '334455', 'Info3'
Data Being Returned for both Lag and Lead
2, 'Mr. Jones', 'Mr. Jones', 'Mr. Jones', '223344', 'Info2'
I need 'Mr. Smith' to be returned for Lag and 'Mr. Davis' returned for Lead
您的查询确实没有输出您所说的。您 table 中的示例数据必须比您向我们展示的更多。
在 table 中使用此数据:
field1 field2 field3 field4
1 Mr. Smith 112233 Info1
2 Mr. Jones 223344 Info2
3 Mr. Davis 334455 Info3
这是您查询的输出:
field1 field2 Lead Lag field3 field4
2 Mr. Jones No Lead No Lag 223344 Info2
符合预期。您在 WHERE 条件下限制了您的集合,仅从您的基础 table 中选择了 1 行。因此,没有 LAG 或 LEAD 行,并且都 return 您给定的默认值。
在您的实际样本 table 中,我确定您有几行 field3 = '223344',但所有的 field2 值都相同吗?
您需要在 FULL 集上使用 window 函数,然后再限制您的查询。这可以通过子查询来完成。在本例中,我使用了一个常见的 table 表达式,我认为它更易于阅读和测试。
WITH
BaseQuery AS (
SELECT p.field1,
p.field2,
LEAD( p.field2, 1, 'No Lead') OVER (ORDER BY p.field1) AS 'Lead',
LAG( p.field2, 1, 'No Lag' ) OVER (ORDER BY p.field1) AS 'Lag',
p.field3,
p.field4
FROM fieldTable p
)
SELECT
*
FROM
BaseQuery
WHERE
field3 = '223344'
这会产生所需的输出:
field1 field2 Lead Lag field3 field4
2 Mr. Jones Mr. Davis Mr. Smith 223344 Info2
我无法将 Lead/Lag 函数正确地 increment/decrement 和 return 正确的值。无论我做什么,它仍然是 return 当前行。 当 p.field3 值 = '223344' 时,我需要显示先前的 p.field2 值和后续的 p.field2 值。
BEGIN TRY
SELECT p.field1,
p.field2,
LEAD( p.field2, 1, 'No Lead') OVER (ORDER BY p.field1) AS 'Lead',
LAG( p.field2, 1, 'No Lag' ) OVER (ORDER BY p.field1) AS 'Lag',
p.field3,
p.field4
FROM fieldTable p
WHERE p.field3 = '223344'
ORDER BY p.field1
END TRY
BEGIN CATCH
.....
END CATCH
Example Data in Database
1, 'Mr. Smith', '112233', 'Info1'
2, 'Mr. Jones', '223344', 'Info2'
3, 'Mr. Davis', '334455', 'Info3'
Data Being Returned for both Lag and Lead
2, 'Mr. Jones', 'Mr. Jones', 'Mr. Jones', '223344', 'Info2'
I need 'Mr. Smith' to be returned for Lag and 'Mr. Davis' returned for Lead
您的查询确实没有输出您所说的。您 table 中的示例数据必须比您向我们展示的更多。
在 table 中使用此数据:
field1 field2 field3 field4
1 Mr. Smith 112233 Info1
2 Mr. Jones 223344 Info2
3 Mr. Davis 334455 Info3
这是您查询的输出:
field1 field2 Lead Lag field3 field4
2 Mr. Jones No Lead No Lag 223344 Info2
符合预期。您在 WHERE 条件下限制了您的集合,仅从您的基础 table 中选择了 1 行。因此,没有 LAG 或 LEAD 行,并且都 return 您给定的默认值。
在您的实际样本 table 中,我确定您有几行 field3 = '223344',但所有的 field2 值都相同吗?
您需要在 FULL 集上使用 window 函数,然后再限制您的查询。这可以通过子查询来完成。在本例中,我使用了一个常见的 table 表达式,我认为它更易于阅读和测试。
WITH
BaseQuery AS (
SELECT p.field1,
p.field2,
LEAD( p.field2, 1, 'No Lead') OVER (ORDER BY p.field1) AS 'Lead',
LAG( p.field2, 1, 'No Lag' ) OVER (ORDER BY p.field1) AS 'Lag',
p.field3,
p.field4
FROM fieldTable p
)
SELECT
*
FROM
BaseQuery
WHERE
field3 = '223344'
这会产生所需的输出:
field1 field2 Lead Lag field3 field4
2 Mr. Jones Mr. Davis Mr. Smith 223344 Info2