简单问题,新建指定间隔数的表
请教如何用cte即with as新建id号指定间隔的表,如:
指定间隔2行
表1
id
1
4
7
10
.
.
.
指定间隔3行
表2
id
1
5
9
13
...
谢谢各位
[解决办法]
DECLARE @i INT
SET @i=2
;WITH cte AS
(
SELECT number
from master..spt_values
WHERE type='p' AND number>0
AND number %(@i+1)=1
)
SELECT * FROM cte
/*
number
-----------
1
4
7
10
13
16
19
22
25
28
31
34
37
40
43
*/
DECLARE @i INT
SET @i=3 --隔开3行
;WITH t AS
(
SELECT 1 as number
union all
select number + @i+1
from t
WHERE number < 10000
)
SELECT *
FROM t
where number < 50
option(maxrecursion 10000)
/*
number
1
5
9
13
17
21
25
29
33
37
41
45
49
*/