We help IT Professionals succeed at work.

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

Kristie
Kristie asked
on
Medium Priority
759 Views
Last Modified: 2012-06-27
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
Comment
Watch Question

Commented:
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)
Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
the above is not what I am looking for.  Please review my questions and table example.
CERTIFIED EXPERT
Top Expert 2012

Commented:
Please post your table schema and a SQL Script to populate the data.

Author

Commented:
ewangoya query works but takes a very long time to loop through all the records.  
G Trurab KhanSnr. Development Manager
CERTIFIED EXPERT

Commented:
Try attached SQL sql1.sql
CERTIFIED EXPERT
Top Expert 2012

Commented:
>> 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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.