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

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

    This is the query I found useful (after modifications of-course):

    DECLARE @numberofitemsperpage INT DECLARE @numberofpages INT DECLARE @currentpage int

    DECLARE @countRecords float SET @countRecords = (Select COUNT(*) From sz_hold_visitsData) -- the Excel can hold approximately ONE MILLION records at a time. if @countRecords >= 1000000 SET @numberofitemsperpage = 500000 ELSE IF @countRecords < 1000000 AND @countRecords >= 500000 SET @numberofitemsperpage = 250000 ELSE IF @countRecords < 500000 AND @countRecords >= 100000 SET @numberofitemsperpage = 50000 ELSE SET @numberofitemsperpage = 10000

    DECLARE @numberofpages_deci float SET @numberofpages_deci = @countRecords / @numberofitemsperpage

    SET @numberofpages = CEILING(@numberofpages_deci) Select @countRecords AS countRecords, @numberofitemsperpage AS numberofitemsperpage , @numberofpages_deci AS numberofpages_deci, @numberofpages AS numberofpagesFnl

    SET @currentpage =0 WHILE @currentpage < @numberofpages BEGIN SELECT a.* FROM (SELECT row_number() OVER (ORDER BY person_ID) AS ROW, * FROM sz_hold_visitsData) a WHERE ROW >= @currentpage * @numberofitemsperpage +1 AND Row <= (@currentpage+1) * @numberofitemsperpage

    IF @@ROWCOUNT = 0 BREAK SET @currentpage = @currentpage +1 END

    In this extract, "sz_hold_visitsData" is a table in my database, whilst "person_ID" is a column therein. You can also further modify the script to output to file:

    DECLARE @numberofitemsperpage INT DECLARE @numberofpages INT DECLARE @currentpage int

    DECLARE @countRecords float SET @countRecords = (Select COUNT(*) From sz_hold_visitsData) -- the Excel can hold approximately ONE MILLION records at a time. if @countRecords >= 1000000 SET @numberofitemsperpage = 500000 ELSE IF @countRecords < 1000000 AND @countRecords >= 500000 SET @numberofitemsperpage = 250000 ELSE IF @countRecords < 500000 AND @countRecords >= 100000 SET @numberofitemsperpage = 50000 ELSE SET @numberofitemsperpage = 10000

    DECLARE @numberofpages_deci float SET @numberofpages_deci = @countRecords / @numberofitemsperpage

    SET @numberofpages = CEILING(@numberofpages_deci) Select @countRecords AS countRecords, @numberofitemsperpage AS numberofitemsperpage , @numberofpages_deci AS numberofpages_deci, @numberofpages AS numberofpagesFnl

    DECLARE @sevrName nvarchar(50) SET @sevrName = '.\sql14' DECLARE @outputFile nvarchar(500)

    SET @currentpage =0 WHILE @currentpage < @numberofpages BEGIN --SELECT a.* FROM (SELECT row_number() OVER (ORDER BY person_ID) AS ROW, * FROM sz_hold_visitsData) a WHERE ROW >= @currentpage * @numberofitemsperpage +1 AND Row <= (@currentpage+1) * @numberofitemsperpage SET @outputFile = 'C:\PSM\outVisits_' +convert(nvarchar(50), @currentpage) + '.csv' --Select @outputFile --TEST

    DECLARE @cmd_ varchar(500) = 'sqlcmd -S ' + @sevrName + ' -E -Q "SELECT a.* FROM (SELECT row_number() OVER (ORDER BY person_ID) AS ROW, * FROM sz_hold_visitsData) a WHERE ROW >= '+ CONVERT(nvarchar(500),@currentpage * @numberofitemsperpage +1) +' AND Row <= ' + CONVERT(nvarchar(500),((@currentpage+1) * @numberofitemsperpage)) +'" -s "," -o ' +@outputFile +' ' -- "C:\PSM\outVisits.csv" ' EXEC xp_cmdshell @cmd_

    IF @@ROWCOUNT = 0 BREAK SET @currentpage = @currentpage +1 END

    Hope helps.

提交回复
热议问题