Technical Article

Another way to split a recordset

,

This way to split a recorset is simpler by using the NTILE() function.

Enjoi it.

USE AdventureWorksDW;
GO
DECLARE @t INT, @bt INT,@dv INT
SET @dv=8000
SELECT @bt=COUNT(*) FROM dbo.FactFinance
IF (@bt%@dv)<>0
BEGIN
SET @t=@bt/@dv + 1
END
ELSE
BEGIN
SET @t=@bt/@dv 
END

SELECT *
,NTILE(@t) OVER(ORDER BY TimeKey ASC) AS 'splitter'
INTO #TEMP
FROM dbo.FactFinance

WHILE @t>0
BEGIN
SELECT [TimeKey]
,[OrganizationKey]
,[DepartmentGroupKey]
,[ScenarioKey]
,[AccountKey]
,[Amount]
FROM #TEMP WHERE splitter=@t
SET @t=@t-1
CONTINUE
END
DROP TABLE #TEMP

Rate

2.83 (6)

You rated this post out of 5. Change rating

Share

Share

Rate

2.83 (6)

You rated this post out of 5. Change rating