Is there a way to split the results of a select query into two equal halfs?

后端 未结 6 1760
无人及你
无人及你 2020-12-28 17:19

I need a solution for a select query in Sql Server 2005.

I\'d like to have a query returning two ResultSets each of which holding exactly half of all records matchin

6条回答
  •  没有蜡笔的小新
    2020-12-28 17:51

    You could use these two queries:

    SELECT * FROM (
        SELECT *, ROW_NUMBER() OVER (ORDER BY TheID) AS rn FROM TheTable
    ) T1
    WHERE rn % 2 = 0
    
    SELECT * FROM (
        SELECT *, ROW_NUMBER() OVER (ORDER BY TheID) AS rn FROM TheTable
    ) T1
    WHERE rn % 2 = 1
    

提交回复
热议问题