如何对Stack Exchange Data Explorer(SEDE)结果进行分页?

时间:2018-08-16 19:54:34

标签: sql-server tsql dataexplorer

使用data explorer创建查询:

SELECT P.id, creationdate,tags,owneruserid,answercount
--SELECT DISTINCT TAGNAME ,TAGID
FROM TAGS  AS T
JOIN POSTTAGS AS PT
ON T.ID = PT.TAGID
JOIN POSTS AS P
ON PT.POSTID = P.ID
--WHERE CAST(P.TAGS AS VARCHAR) IN('JAVA')
WHERE PT.TAGID = 3143

如何在查询中添加分页以便不仅获取前50,000个结果,然后再次运行查询以获取下一个剩余结果?

1 个答案:

答案 0 :(得分:1)

有几种方法可以通过TSQL结果进行“分页”;看到:

在这里,我将使用CTE方法:

  • 它使用方便的行号来翻页结果,而不是尝试跟踪诸如creationdate之类难以预测的因素。
  • 据报道它的执行速度比OFFSET方法快。

因此,该问题的查询变为this SEDE query

-- StartRow: Starting row for paging
-- EndRow: Ending row for paging (Max 50K rows at a time)
WITH allData AS (
    SELECT
                ROW_NUMBER() OVER (ORDER BY P.creationdate) AS row
                , P.id
                , P.creationdate
                , P.tags
                , P.owneruserid
                , P.answercount
    FROM        Posttags    AS PT
    JOIN        Posts       AS P    ON PT.postid = P.id
    WHERE       PT.tagid    = 3143  -- tag [scala]
)
SELECT      *
FROM        allData
WHERE       row    >= ##StartRow:INT?1##
AND         row    <= ##EndRow:INT?50000##
ORDER BY    row