扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:赛迪网 李丝 来源:天新网 2008年3月22日
关键字: 数据库 SQL Server Mssql SQL
在本页阅读全文(共6页)
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
SQL> exec :n := 5;
PL/SQL 过程已成功完成。
SQL> /
STR
------------------------------------------------------
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
SQL>
我们可以尝试填足一下:
代码:--------------------------------------------------
SQL> exec :n := 5
PL/SQL 过程已成功完成。
SQL> 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 :n - (i - j) || "1" || i
12 when i + j - :n - 1 then
13 :n - (i + j - :n - 1) || "2" || j
14 when j - i then
15 :n - (j - i) || "3" || (:n - i)
16 when :n - i - j + 1 then
17 :n - (:n - i - j + 1) || "4" || 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;
STR
-----------------------------------------------------------
7
8 12 6
1 9 13 11 5
2 10 4
3
SQL> exec :n := 7;
PL/SQL 过程已成功完成。
SQL> /
STR
-----------------------------------------------------------
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。