Home » Other » General » Puzzle n°00 - Row generator *
Puzzle n°00 - Row generator * [message #291171] Thu, 03 January 2008 04:37 Go to next message
Michel Cadot
Messages: 17653
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
The purpose of this puzzle is to wrap-up all SQL or PL/SQL techniques to generate rows.

Enjoy!

Regards
Michel
Re: Puzzle n°00 - Row generator * [message #291177 is a reply to message #291171 ] Thu, 03 January 2008 04:52 Go to previous messageGo to next message
Michel Cadot
Messages: 17653
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
SQL 8i
SQL> DEFINE N=3
SQL> SELECT ROWNUM 
  2  FROM ( SELECT 1 FROM DUAL GROUP BY CUBE(1,2,3,4,5,6,7,8,9,10) )
  3  WHERE ROWNUM <= &N
  4  /
    ROWNUM
----------
         1
         2
         3

SQL 9i
SQL> SELECT * 
  2  FROM ( SELECT ROWNUM FROM DUAL CONNECT BY LEVEL <= &N )
  3  /
    ROWNUM
----------
         1
         2
         3

SQL 10g
SQL> SELECT ROWNUM FROM DUAL CONNECT BY LEVEL <= &N;
    ROWNUM
----------
         1
         2
         3

Using a pipeline function (9i)
SQL> create or replace type row_table as table of number;
  2  /

Type created.

SQL> Create or replace function gen_row (num_rows in number) 
  2  return row_table 
  3  parallel_enable pipelined is 
  4  begin 
  5    for x in 1..num_rows loop
  6      pipe row (x);
  7    end loop;
  8    return;
  9  end; 
 10  / 

Function created.

SQL> select * from table(gen_row(&N)); 
COLUMN_VALUE
------------
           1
           2
           3

3 rows selected.

I know there are other ways but couldn't remember now, especially there is a way using XML query, if someone can post it.

Regards
Michel
Re: Puzzle n°00 - Row generator * [message #291190 is a reply to message #291171 ] Thu, 03 January 2008 05:43 Go to previous messageGo to next message
hobbes
Messages: 173
Registered: January 2006
Senior Member
This is on 10g.
SQL> define n=5
SQL> select to_number(column_value) colval
  2  from xmltable('for $i in 1 to &n return $i');

    COLVAL
----------
         1
         2
         3
         4
         5
Re: Puzzle n°00 - Row generator * [message #291199 is a reply to message #291171 ] Thu, 03 January 2008 06:19 Go to previous messageGo to next message
rajavu1
Messages: 1075
Registered: May 2005
Location: Bangalore
Senior Member

This is also in 10g:

SQL > select integer_value
  2   from   dual
  3  where  1=2
  4  model
  5  dimension by ( 0 as key )
  6  measures     ( 0 as integer_value )
  7  rules upsert ( integer_value[ for key from 1 to 5 increment 1 ] = cv(key) );

INTEGER_VALUE
-------------
            1
            2
            3
            4
            5


Thumbs Up
Rajuvan.

[Edit MC: change 10 to 5 to remove superfluous lines, please use a variable to show us what part is dependent on the parameter]

[Updated on: Thu, 03 January 2008 07:02] by Moderator

Re: Puzzle n°00 - Row generator * [message #291203 is a reply to message #291171 ] Thu, 03 January 2008 06:40 Go to previous messageGo to next message
rajavu1
Messages: 1075
Registered: May 2005
Location: Bangalore
Senior Member

And why the conventional method of generating limitted rows has been missed out ?

Might be from Oracle 3 :

SQL> select rownum from all_objects where rownum<=5;

    ROWNUM
----------
         1
         2
         3
         4
         5


Thumbs Up
Rajuvan.

[Updated on: Thu, 03 January 2008 07:01] by Moderator

Re: Puzzle n°00 - Row generator * [message #291216 is a reply to message #291203 ] Thu, 03 January 2008 07:19 Go to previous messageGo to next message
Maaher
Messages: 6685
Registered: May 2002
Senior Member
Perhaps a link to the Wiki page is in its place here?

Oracle Row Generator Techniques

I encourage you all to add missing techniques to that page too.

MHE
Re: Puzzle n°00 - Row generator * [message #291223 is a reply to message #291216 ] Thu, 03 January 2008 07:42 Go to previous messageGo to next message
Michel Cadot
Messages: 17653
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Ah ah! Ravujan is a little cheater! Copying and pasting the wiki.

Regards
Michel
Re: Puzzle n°00 - Row generator * [message #291224 is a reply to message #291171 ] Thu, 03 January 2008 07:46 Go to previous messageGo to next message
Michel Cadot
Messages: 17653
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Another MODEL query (>= 10g)

SQL> select val
  2  from (select 1 rn from dual)
  3  model
  4    dimension by (rn)
  5    measures (1 val)
  6  rules 
  7    iterate (&N)
  8    ( val[iteration_number] = iteration_number+1 )
  9  order by val
 10  /
       VAL
----------
         1
         2
         3
         4
         5

Regards
Michel
Re: Puzzle n°00 - Row generator * [message #291327 is a reply to message #291171 ] Thu, 03 January 2008 22:35 Go to previous messageGo to next message
rajavu1
Messages: 1075
Registered: May 2005
Location: Bangalore
Senior Member

Hi Michel ,


Sorry For my late response as i left yesterday before your post and net connection was damn slow.

I didn't want to CHEAT anybody with my post. I admitted already here that I did Copy amd Paste for asnwering this thread .

My only intention was to include the usage of MODEL which was missed out in the Topic , which I believe is purpose of the Puzzle Forum ( Please Forgive me if you feel this is cheating and against the public interest ) .

I am not good with Oracle 10g Techniques as Michel is (Really).

Now I feel I could paste the link instead .

Thumbs Down
Rajuvan .

[Updated on: Thu, 03 January 2008 22:39]

Re: Puzzle n°00 - Row generator * [message #291355 is a reply to message #291327 ] Thu, 03 January 2008 23:55 Go to previous message
Michel Cadot
Messages: 17653
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Quote:

Now I feel I could paste the link instead

Yes, this was also my feeling not instead but in addition.
Without the link, it seems that you claimed it.
But you're right (in your other answer) this is a wrap-up.

Regards
Michel
Previous Topic:Puzzle n°05 - All the possible ways to get the factorial of a number *
Next Topic:Server Virtualization
Goto Forum:
  


Current Time: Thu Jul 24 15:11:44 CDT 2008

Total time taken to generate the page: 0.02514 seconds
.:: Forum Home :: Site Home :: Wiki Home :: Contact :: Privacy ::.