如何在 SEDE 上对徽章 table 进行分页?
How to paginate the badge table on SEDE?
使用 this 示例中的代码:
-- UserId: User ID "Enter a user's ID."
SELECT u.Id AS [User Link]
, CASE
WHEN b.Tagbased = 1 THEN 'Tag: [' + b.Name + ']'
ELSE b.Name
END AS [Badge name]
, CASE
WHEN b.Class = 1 THEN 'Gold'
WHEN b.Class = 2 THEN 'Silver'
WHEN b.Class = 3 THEN 'Bronze'
ELSE '*Unkown*'
END AS [Class]
, b.Date AS [Awarded]
FROM Badges b
LEFT JOIN Users u ON u.Id = b.Userid
--WHERE b.Userid = ##UserId:int##
WHERE u.reputation > 100000
ORDER BY u.Displayname
, [Awarded] DESC
如何实现分页 示例以接收超过 50000 的结果?
使用 CTE 方法(据称比 OFFSET 和 FETCH 更快),查询变为:
(看得见live at SEDE)
-- StartRow: Starting row for paging
-- EndRow: Ending row for paging (Max 50K rows at a time)
-- MinimumRep: Ony return data for users that have this many reputation points
WITH allData AS (
SELECT
ROW_NUMBER() OVER (ORDER BY u.Displayname, b.Date DESC) AS row
, u.Id
, CASE
WHEN b.Tagbased = 1 THEN 'Tag: [' + b.Name + ']'
ELSE b.Name
END AS [Badge name]
, CASE
WHEN b.Class = 1 THEN 'Gold'
WHEN b.Class = 2 THEN 'Silver'
WHEN b.Class = 3 THEN 'Bronze'
ELSE '*Unkown*'
END AS [Class]
, b.Date AS [Awarded]
FROM Users u
INNER JOIN Badges b ON u.Id = b.Userid
WHERE u.reputation > ##MinimumRep:int?100000##
)
SELECT a.Id AS [User Link]
, a.[Badge name]
, a.[Class]
, a.[Awarded]
FROM allData a
WHERE row >= ##StartRow:INT?1##
AND row <= ##EndRow:INT?50000##
ORDER BY row
使用 this 示例中的代码:
-- UserId: User ID "Enter a user's ID."
SELECT u.Id AS [User Link]
, CASE
WHEN b.Tagbased = 1 THEN 'Tag: [' + b.Name + ']'
ELSE b.Name
END AS [Badge name]
, CASE
WHEN b.Class = 1 THEN 'Gold'
WHEN b.Class = 2 THEN 'Silver'
WHEN b.Class = 3 THEN 'Bronze'
ELSE '*Unkown*'
END AS [Class]
, b.Date AS [Awarded]
FROM Badges b
LEFT JOIN Users u ON u.Id = b.Userid
--WHERE b.Userid = ##UserId:int##
WHERE u.reputation > 100000
ORDER BY u.Displayname
, [Awarded] DESC
如何实现分页
使用 CTE 方法(据称比 OFFSET 和 FETCH 更快),查询变为:
(看得见live at SEDE)
-- StartRow: Starting row for paging
-- EndRow: Ending row for paging (Max 50K rows at a time)
-- MinimumRep: Ony return data for users that have this many reputation points
WITH allData AS (
SELECT
ROW_NUMBER() OVER (ORDER BY u.Displayname, b.Date DESC) AS row
, u.Id
, CASE
WHEN b.Tagbased = 1 THEN 'Tag: [' + b.Name + ']'
ELSE b.Name
END AS [Badge name]
, CASE
WHEN b.Class = 1 THEN 'Gold'
WHEN b.Class = 2 THEN 'Silver'
WHEN b.Class = 3 THEN 'Bronze'
ELSE '*Unkown*'
END AS [Class]
, b.Date AS [Awarded]
FROM Users u
INNER JOIN Badges b ON u.Id = b.Userid
WHERE u.reputation > ##MinimumRep:int?100000##
)
SELECT a.Id AS [User Link]
, a.[Badge name]
, a.[Class]
, a.[Awarded]
FROM allData a
WHERE row >= ##StartRow:INT?1##
AND row <= ##EndRow:INT?50000##
ORDER BY row