How to paginate the badge table on SEDE?

假如想象 提交于 2021-01-28 08:14:24

问题


Having the code from this example:

-- 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

How is it possible to implement a pagination like this example to receive the results which are more than 50000?


回答1:


Using the CTE method (which is allegedly faster than OFFSET and FETCH), the query becomes:
(See it 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


来源:https://stackoverflow.com/questions/56890083/how-to-paginate-the-badge-table-on-sede

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!