扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:赛迪网 李丝 来源:天新网 2008年3月22日
关键字: 数据库 Mssql SQL Server SQL
在本页阅读全文(共6页)
11 19 9
12 20 24 18 8
1 13 21 25 23 17 7
2 14 22 16 6
3 15 5
4
已选择7行。
SQL> exec :n := 9;
PL/SQL 过程已成功完成。
SQL> /
STR
---------------------------------------------------------------
13
14 26 12
15 27 35 25 11
16 28 36 40 34 24 10
1 17 29 37 41 39 33 23 9
2 18 30 38 32 22 8
3 19 31 21 7
4 20 6
5
已选择9行。
SQL> exec :n := 8
PL/SQL 过程已成功完成。
SQL> /
STR
------------------------------------------------------
5 4
6 18 17 3
7 19 27 26 16 2
8 20 28 32 31 25 15 1
9 21 29 30 24 14
10 22 23 13
11 12
对于比较大的N值, 需对"顺时针螺旋特征码"的组成进行适当修改:
代码:----------------------------------------------------
1 select replace(max(sys_connect_by_path(rank, ",")), ",") str
2 from (select i, j,
3 case when rank() over(order by tag) - floor(:n * :n / 2) <= 0 then " "
4 else to_char(rank() over(order by tag) - floor(:n * :n / 2), "9999") end as rank,
5 min(j) over(partition by i) minj
6 from (select i,
7 j,
8 -- 逆时针螺旋特征码 counter-clockwise
9 case greatest(i - j, i + j - :n - 1, j - i, :n - i - j + 1)
10 when i - j then
11 chr(:n - (i - j)) || "1" || chr(i)
12 when i + j - :n - 1 then
13 chr(:n - (i + j - :n - 1)) || "2" || chr(j)
14 when j - i then
15 chr(:n - (j - i)) || "3" || chr((:n - i))
16 when :n - i - j + 1 then
17 chr(:n - (:n - i - j + 1)) || "4" || chr(i)
18 end as tag
19 from (select level as i from dual connect by level <= :n) a,
20 (select level as j from dual connect by level <= :n) b
21 -- where abs(i - j) < floor(:n / 2 + .6)
22 -- and i + j between floor(:n / 2 + .6) + 1 and floor(:n / 2 + .6) + :n
23 )
24 )
25 start with j = minj
26 connect by j - 1 = prior j and i = prior i
27 group by i
28* order by i
SQL> /
STR
-----------------------------------------------------
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。