我们如何在 SQL 服务器 2008 R2 中使用 LAG()?
How can we use LAG() in SQL server 2008 R2?
SELECT
CASE
WHEN ser.[Department Name] = LAG(SER.[Department Name]) OVER (ORDER BY SER.[Department Name]) THEN ''
ELSE SER.[Department Name]
END [Department Name] from department
您可以使用 APPLY
:
SELECT (CASE WHEN d.[Department Name] = Prev_Dept
THEN '' ELSE d.[Department Name]
END) AS DepartmentName
FROM department d OUTER APPLY
( SELECT TOP (1) d1.[Department Name] AS Prev_Dept
FROM department d1
WHERE d1.? < d.?
ORDER BY ? DESC
) AS Pd;
?
表示标识唯一数据序列的标识列。
SELECT
CASE
WHEN ser.[Department Name] = LAG(SER.[Department Name]) OVER (ORDER BY SER.[Department Name]) THEN ''
ELSE SER.[Department Name]
END [Department Name] from department
您可以使用 APPLY
:
SELECT (CASE WHEN d.[Department Name] = Prev_Dept
THEN '' ELSE d.[Department Name]
END) AS DepartmentName
FROM department d OUTER APPLY
( SELECT TOP (1) d1.[Department Name] AS Prev_Dept
FROM department d1
WHERE d1.? < d.?
ORDER BY ? DESC
) AS Pd;
?
表示标识唯一数据序列的标识列。