Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple LOOP

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-09-25 : 17:07:11
Hi All - I have the following table:

USER_ID QTY
jsmith 2
rbird 4
lsantore 1

I need the USER_ID to repeat on multiple lines based on the QTY:

USER_ID
jsmith
jsmith
rbird
rbird
rbird
rbird
lsantore

Any ideas how I can achieve this? Thanks!

SELECT USER_ID FROM TABLE1

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-09-25 : 17:19:30
[code]DECLARE @Foo TABLE (USER_ID VARCHAR(128), QTY INT)
INSERT @Foo VALUES
('jsmith', 2),
('rbird', 4),
('lsantore', 1)


SELECT T.USER_ID
FROM @Foo AS T
INNER JOIN
master..spt_values AS A
ON A.number <= T.Qty
aND A.Type = 'P'

[/code]
Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-09-25 : 17:34:20
WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!! THANKS!
Go to Top of Page
   

- Advertisement -