Home » SQL & PL/SQL » SQL & PL/SQL » Repeat Rows based on column value (merged by CM)
Repeat Rows based on column value (merged by CM) [message #456775] Wed, 19 May 2010 08:38 Go to next message
cplib
Messages: 15
Registered: March 2010
Junior Member
Hi

I have a select statement

select name, address, qty
from names_table


and it returns a result such as

name|address|qty
alan,1 the street,5
bert,2 the road,3

what I want to do is create an output that takes the value in the qty column and repeates the number of rows e.g. the alan record 5 times and the bert record 3 times

name|address|qty
alan,1 the street,5
alan,1 the street,5
alan,1 the street,5
alan,1 the street,5
alan,1 the street,5
bert,2 the road,3
bert,2 the road,3
bert,2 the road,3

Can anyone suggest how I might go about acheiving this?
Re: Repeat Rows based on column value (merged by CM) [message #456784 is a reply to message #456775] Wed, 19 May 2010 08:46 Go to previous messageGo to next message
cplib
Messages: 15
Registered: March 2010
Junior Member
apologies if the OP appeared twice, the message is still not showing as posted in another browser window I have open.
Re: Repeat Rows based on column value (merged by CM) [message #456786 is a reply to message #456775] Wed, 19 May 2010 08:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Search here for "row generator" and come back if you don't find the solution (after trying to find it).

Regards
Michel
Re: Repeat Rows based on column value (merged by CM) [message #456794 is a reply to message #456786] Wed, 19 May 2010 08:55 Go to previous messageGo to next message
cplib
Messages: 15
Registered: March 2010
Junior Member
Hi Michel

Thanks for your reply, I have had a look at the link and the wiki, but neither of them seem to make reference to using the results of the select statement to determine the number of rows.

Is there anything else you could suggest?
Re: Repeat Rows based on column value (merged by CM) [message #456796 is a reply to message #456794] Wed, 19 May 2010 08:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Yes, you can do it with what you read as you can join a row generator with your table.
At a first step just try a simple cartesian product, then you will find how to restrict to the number of rows you want.

Regards
Michel

[Updated on: Wed, 19 May 2010 08:57]

Report message to a moderator

Re: Repeat Rows based on column value (merged by CM) [message #456799 is a reply to message #456786] Wed, 19 May 2010 09:17 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Link (unless I missed it) did not mention this method:

with t as (
           select 'alan' name,'1 the street' address,5 qty from dual union all
           select 'bert','2 the road',3 from dual
          )
select  t.*
  from  t,
        table(cast(multiset(select 1 from dual connect by level <= qty) as sys.OdciNumberList))
/

NAME ADDRESS              QTY
---- ------------ -----------
alan 1 the street           5
alan 1 the street           5
alan 1 the street           5
alan 1 the street           5
alan 1 the street           5
bert 2 the road             3
bert 2 the road             3
bert 2 the road             3

8 rows selected.

SQL> 


SY.
Re: Repeat Rows based on column value (merged by CM) [message #456823 is a reply to message #456799] Wed, 19 May 2010 10:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Note that sys.OdciNumberList is documented only in 10.2 and up.

Regards
Michel
Re: Repeat Rows based on column value (merged by CM) [message #456828 is a reply to message #456823] Wed, 19 May 2010 10:51 Go to previous messageGo to next message
cplib
Messages: 15
Registered: March 2010
Junior Member
I am using Oracle 9i
Re: Repeat Rows based on column value (merged by CM) [message #456830 is a reply to message #456828] Wed, 19 May 2010 10:52 Go to previous messageGo to next message
ramoradba
Messages: 2456
Registered: January 2009
Location: AndhraPradesh,Hyderabad,I...
Senior Member
So what Michel already provided you the solution read the Link
sriram Smile
Re: Repeat Rows based on column value (merged by CM) [message #456831 is a reply to message #456828] Wed, 19 May 2010 10:54 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/

>I am using Oracle 9i
NOW you tell us. Sad

V9 is obsoleted & unsupported. Please join the 21st century at your earliest convenience.
Re: Repeat Rows based on column value (merged by CM) [message #456832 is a reply to message #456823] Wed, 19 May 2010 11:01 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Wed, 19 May 2010 11:24
Note that sys.OdciNumberList is documented only in 10.2 and up.


Don't care much about desupported versions, but if someone is still using it they can change type to something like SYS.DBMS_DEBUG_VC2COLL.

SY.
P.S. It is documented starting 10.1, not 10.2.

[Updated on: Wed, 19 May 2010 11:06]

Report message to a moderator

Re: Repeat Rows based on column value (merged by CM) [message #456833 is a reply to message #456830] Wed, 19 May 2010 11:09 Go to previous messageGo to next message
cplib
Messages: 15
Registered: March 2010
Junior Member
Sorry for not mentioning the version.

I am reading the link provided and I understand how to create a output with a number of rows based on using the CUBE function, but cant see how I can achieve what I specifically require.

Could you perhaps point me at the specific park of the link I should be studying?

Thanks again,
Re: Repeat Rows based on column value (merged by CM) [message #456835 is a reply to message #456833] Wed, 19 May 2010 11:21 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
cplib wrote on Wed, 19 May 2010 12:09
Sorry for not mentioning the version.


Then use:

SQL> with t as (
  2             select 'alan' name,'1 the street' address,5 qty from dual union all
  3             select 'bert','2 the road',3 from dual
  4            )
  5  select  t.*
  6    from  t,
  7          table(cast(multiset(select '1' from dual connect by level <= qty) as sys.dbms_debug_vc2coll))
  8  /

NAME ADDRESS                               QTY
---- ------------ ----------------------------
alan 1 the street                            5
alan 1 the street                            5
alan 1 the street                            5
alan 1 the street                            5
alan 1 the street                            5
bert 2 the road                              3
bert 2 the road                              3
bert 2 the road                              3

8 rows selected.

SQL> 


SY.

[Updated on: Wed, 19 May 2010 11:22]

Report message to a moderator

Re: Repeat Rows based on column value (merged by CM) [message #456840 is a reply to message #456832] Wed, 19 May 2010 11:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
P.S. It is documented starting 10.1, not 10.2.

Quote:
Don't care much about desupported versions

Nothing more to say than what you said.

Just one thing, if I have to choose between 9.2 and 10.1, I choose 9.2.

Regards
Michel
Re: Repeat Rows based on column value (merged by CM) [message #456842 is a reply to message #456835] Wed, 19 May 2010 11:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
Then use:

Please let people do their homework otherwise:
1/ They will never learn
2/ They will post on forum again and again to get someone else makes their job

Regards
Michel
Re: Repeat Rows based on column value (merged by CM) [message #688065 is a reply to message #456775] Tue, 15 August 2023 16:54 Go to previous message
M117
Messages: 1
Registered: August 2023
Junior Member
Another option would be to use connect by level with cross apply.

with t as (
           select 'alan' name,'1 the street' address,5 qty from dual union all
           select 'bert','2 the road',3 from dual
          )
select  t.* 
FROM t 
CROSS APPLY
(select 1 from dual connect by level <= qty) tt
Previous Topic: Last N TRANSACTIONS for each customer (syntax error)
Next Topic: Ranking account and customer balances
Goto Forum:
  


Current Time: Sat Apr 27 08:43:26 CDT 2024