Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: i want to print in single line pl/sql block

Re: i want to print in single line pl/sql block

From: Michel Cadot <micadot{at}altern{dot}org>
Date: Fri, 31 Aug 2007 18:51:52 +0200
Message-ID: <46d8472a$0$5306$426a34cc@news.free.fr>

"Shakespeare" <whatsin_at_xs4all.nl> a écrit dans le message de news: 46d5c26c$0$227$e4fe514c_at_news.xs4all.nl...
|
| "Michel Cadot" <micadot{at}altern{dot}org> schreef in bericht
| news:46d45275$0$407$426a34cc_at_news.free.fr...
| >
| > "saiba" <saibhasker_at_gmail.com> a écrit dans le message de news:
| > 1188296336.903895.292130_at_i38g2000prf.googlegroups.com...
| > | how can i get output like this
| > |
| > | 1 = 1
| > | 1+2 = 3
| > | 1+2+3 = 6
| > | ........
| > | 1+2+3+4+5+6+7+8+9+10 = 55;
| > | i have to print same order.
| > |
| > | i don't got it but i wrote something see and modify and send reply
| > | ---
| > | declare
| > | v number := 0;
| > | v1 number;
| > | begin
| > | for i in 1..10 loop
| > | for j in 1..i loop
| > | v:= v+j;
| > | dbms_output.put_line(j);
| > | if j<>i then
| > | dbms_output.put_line(' + ');
| > | end if;
| > | end loop;
| > | dbms_output.put_line(' = '||v);
| > | v:=0;
| > | end loop;
| > | end;
| > | /
| > |
| >
| > Just for fun:
| >
| > SQL> col operation format a30 justify right
| > SQL> col "SUM" format a5
| > SQL> with data as ( select level lvl from dual connect by level <= 10 )
| > 2 select lpad(substr(sys_connect_by_path(level,'+'),2)||' =',30)
| > operation,
| > 3 ( select to_char(sum(lvl))
| > 4 from data b
| > 5 connect by prior lvl = lvl+1
| > 6 start with b.lvl = a.lvl ) "SUM"
| > 7 from data a
| > 8 connect by prior lvl = lvl-1
| > 9 start with lvl = 1
| > 10 order by lvl
| > 11 /
| > OPERATION SUM
| > ------------------------------ -----
| > 1 = 1
| > 1+2 = 3
| > 1+2+3 = 6
| > 1+2+3+4 = 10
| > 1+2+3+4+5 = 15
| > 1+2+3+4+5+6 = 21
| > 1+2+3+4+5+6+7 = 28
| > 1+2+3+4+5+6+7+8 = 36
| > 1+2+3+4+5+6+7+8+9 = 45
| > 1+2+3+4+5+6+7+8+9+10 = 55
| >
| > 10 rows selected.
| >
| > Regards
| > Michel Cadot
| >
| >
|
| Michel,
|
| A beauty (as always)
|
| Shakespeare
|

Thanks,

Of course, the sum of n first integers is n(n+1)/2:

SQL> with data as ( select level lvl from dual connect by level <= 10 )   2 select lpad(substr(sys_connect_by_path(level,'+'),2),30) operation, '=' "=",   3 to_char(lvl*(lvl+1)/2) "SUM"   4 from data
  5 connect by prior lvl = lvl-1
  6 start with lvl = 1
  7 order by lvl
  8 /

                     OPERATION = SUM
------------------------------ - -----
                             1 = 1
                           1+2 = 3
                         1+2+3 = 6
                       1+2+3+4 = 10
                     1+2+3+4+5 = 15
                   1+2+3+4+5+6 = 21
                 1+2+3+4+5+6+7 = 28
               1+2+3+4+5+6+7+8 = 36
             1+2+3+4+5+6+7+8+9 = 45
          1+2+3+4+5+6+7+8+9+10 = 55

10 rows selected.

But it was funnier to let Oracle calculates the sum.

Regards
Michel Cadot Received on Fri Aug 31 2007 - 11:51:52 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US