我有以下 SQL,我想知道如何分页:
我有变量@skip
,@top
所以我可以翻页..
SELECT ID FROM (
SELECT COUNT(*) AS ID, -1 AS [Weight]
FROM Employees i
INNER JOIN #WeightedIDs w
ON (i.ID = w.ID)
WHERE (i.DepartmentID = 10 and i.ShiftID = 2)
UNION ALL
SELECT i.ID, w.[Weight]
FROM Employees i
INNER JOIN #WeightedIDs w
ON (i.ID = w.ID)
WHERE (i.DepartmentID = 10 and i.ShiftID = 2)
) x
ORDER BY x.[Weight] ASC
更新:
我有以下内容,但它根本不起作用:
DECLARE @skip INT, @top INT
SET @skip = 30
SET @top = 100
;WITH PaginatedResults AS
(
SELECT ID, w.[Weight],
ROW_NUMBER() OVER (ORDER BY w.[Weight] ASC) AS RowNum
FROM Employees i
INNER JOIN #WeightedIDs w
ON (i.ID = w.ID)
WHERE (i.DepartmentID = 10 and i.ShiftID = 2)
)
SELECT ID FROM (
SELECT COUNT(*) AS ID, -1 AS [Weight]
FROM Employees i
INNER JOIN #WeightedIDs w
ON (i.IssueID = w.id)
WHERE FlightID > 2 and IssueID > 0
UNION ALL
SELECT ID, [Weight]
FROM PaginatedResults
WHERE RowNum >= @skip AND RowNum < @skip + @top
) x
ORDER BY x.[Weight] ASC