你能解释一下这些代码行的作用吗?
表 1:INT、VARCHAR、FLOAT
ID Name value
---------------------------
1 a1 32116580
2 a2 50785384
3 a3 54327508
4 a4 61030844
;with coords(i,row,col,total,N) as (
select 1,1,1,N.N*(N.N+1)/2, N.N
from (select count(*) N from table1) N
union all
select i+1,
case when col+1>N then row+1 else row end,
case when col+1>N then row+1 else col+1 end,
total, N
from coords
where i<total
)
我知道with
提供了一种编写辅助语句以用于更大查询的方法,所以就像我声明一些我会使用的变量一样,但在那之后我有点困惑......以及为什么使用case
for get row and col
; 另外为什么在这种情况下有两个: case when col+1>N then row+1 else
,SQL怎么知道when to do one case or the other
?...
i row col total N
--------------------
1 1 1 10 4
2 1 2 10 4
3 1 3 10 4
4 1 4 10 4
5 2 2 10 4
6 2 3 10 4
7 2 4 10 4
8 3 3 10 4
9 3 4 10 4
10 4 4 10 4