SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple LOOP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

funk.phenomena
Posting Yak Master

Canada
120 Posts

Posted - 09/25/2012 :  17:07:11  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4347 Posts

Posted - 09/25/2012 :  17:19:30  Show Profile  Reply with Quote
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'

Go to Top of Page

funk.phenomena
Posting Yak Master

Canada
120 Posts

Posted - 09/25/2012 :  17:34:20  Show Profile  Reply with Quote
WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!! THANKS!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000