Link to home
Start Free TrialLog in
Avatar of Kristie
Kristie

asked on

How to split a single row into multiple rows based upon a number value

I have a quick question. I am trying to split a single row into multiple rows based upon a specific number value. I want the single row to be split every time it has 50 units. So if a row has 150 units in it, the row will be split 3 times, with 50 units as the value for each. Or if the row has 233 units, the row will be split 5 times, 4 rows with 50 units and one row with 33 units.

Example:

Start:

Carrier Units
CBA Carriers 150 units
ABC Carriers 233 units

End:

CBA Carriers 50 units
CBA Carriers 50 units
CBA Carriers 50 units
ABC Carriers 50 units
ABC Carriers 50 units
ABC Carriers 50 units
ABC Carriers 50 units
ABC Carriers 33 units
Avatar of Trixor
Trixor

I wonder if this would help you:

DECLARE @rowsperpage INT

DECLARE @start INT

SET @start = 0
SET @rowsperpage = 50

SELECT * FROM
(
SELECT row_number() OVER (ORDER BY column) AS rownum, column2, column3, .... columnX
FROM   table) AS A
WHERE A.rownum BETWEEN (@start) AND (@start + @rowsperpage) 

Open in new window


(source: http://www.select-sql.com/mssql/how-to-make-limit-from-to-in-mssql-2005.html)
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kristie

ASKER

the above is not what I am looking for.  Please review my questions and table example.
Please post your table schema and a SQL Script to populate the data.
Avatar of Kristie

ASKER

ewangoya query works but takes a very long time to loop through all the records.  
Try attached SQL sql1.sql
>> but takes a very long time to loop through all the records.<<
If you are still using SQL Server 2000, you may not have much choice.