Where The Streets Have No Name

How to display numbers in triangle format 본문

Developement/DataBase

How to display numbers in triangle format

highheat 2008. 1. 25. 22:57
http://forums.oracle.com/forums/thread.jspa?threadID=608730

SQL> var N number
SQL> exec :N := 4

PL/SQL-procedure is geslaagd.

SQL> select lpad(' ',:N-i,' ') || ltrim(x) n
2 from dual
3 model
4 dimension by (1 i)
5 measures (cast('1' as varchar2(100)) x)
6 rules iterate(10000) until iteration_number = :N-1
7 ( x[for i from 2 to :N increment 1] = x[cv()] ||
8 case
9 when iteration_number+1 <= cv(i)
10 then ' ' || to_char(iteration_number+1)
11 end
12 )
13 order by i
14 /

N
----------------------------------------------------------------
1
1 2
1 2 3
1 2 3 4

4 rijen zijn geselecteerd.


SQL> exec :N := 10

PL/SQL-procedure is geslaagd.

SQL> select lpad(' ',:N-i,' ') || ltrim(x) n
2 from dual
3 model
4 dimension by (1 i)
5 measures (cast('1' as varchar2(100)) x)
6 rules iterate(10000) until iteration_number = :N-1
7 ( x[for i from 2 to :N increment 1] = x[cv()] ||
8 case
9 when iteration_number+1 <= cv(i)
10 then ' ' || to_char(iteration_number+1)
11 end
12 )
13 order by i
14 /

N
----------------------------------------------------------------
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

10 rijen zijn geselecteerd.



SQL> var N number
SQL> exec :N := 4

PL/SQL-procedure is geslaagd.

SQL> select lpad(' ',:N-level,' ') || ltrim(sys_connect_by_path(level,' ')) n
2 from dual
3 connect by level <= :N
4 /

N
-------------------------------------------------------------------------------------
1
1 2
1 2 3
1 2 3 4

4 rijen zijn geselecteerd.

SQL> exec :N := 10

PL/SQL-procedure is geslaagd.

SQL> select lpad(' ',:N-level,' ') || ltrim(sys_connect_by_path(level,' ')) n
2 from dual
3 connect by level <= :N
4 /

N
-------------------------------------------------------------------------------------
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

10 rijen zijn geselecteerd.