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

后端 未结 6 1750
无人及你
无人及你 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:06

    Here is another solution:

    You would need to use a temp table to hold the first 50% as below:

    select top 50 percent * 
    into #YourTempTable
    from TheTable 
    
    -- The below would give the first half
    select * from #YourTempTable
    
    -- The below woud give rest of the half 
    select * from TheTable where TheID not in (select TheID from #YourTempTable)
    

提交回复
热议问题