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

后端 未结 6 1747
无人及你
无人及你 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 18:01

    try this:

    DECLARE @CountOf int,@Top int,@Bottom int
    SELECT @CountOf=COUNT(*) FROM YourTable
    SET @Top=@CountOf/2
    SET @Bottom=@CountOf-@Top
    SELECT TOP (@Top) * FROM YourTable ORDER BY 1 asc --assumes column 1 is your PK
    SELECT TOP (@Bottom) * FROM YourTable ORDER BY 1 desc --assumes column 1 is your PK
    

提交回复
热议问题