2015-07-13 134 views
3

我想要一列数字: 整数1的7个发生,其次是7个发生2,然后是7个发生3 ....,然后是7个发生的n -1,然后是n的​​7次出现。像这样递归cte重复几个整数

Num 
1 
1 
1 
1 
1 
1 
1 
2 
2 
2 
2 
2 
2 
2 
... 
... 
n-1 
n-1 
n-1 
n-1 
n-1 
n-1 
n-1 
n 
n 
n 
n 
n 
n 
n 

不幸的是我没有太进步。我现在尝试以下,其中n = 4:

WITH 
    one AS 
     (
      SELECT num = 1, 
        cnt = 0 
      UNION ALL 

      SELECT num = num, 
        cnt = cnt + 1 
      FROM one 
      WHERE cnt < 7    
     ), 
    x AS 
     (
      SELECT num, 
        cnt = 0 
      FROM one 

      UNION ALL 
      SELECT num = num + 1, 
        cnt = cnt + 1 
      FROM one 
      WHERE cnt < 4  
     ) 
SELECT * 
FROM x 
+0

检查我的答案,是不是你所需要的? –

回答

1
;WITH Numbers AS 
(
    SELECT n = 1 
    UNION ALL 
    SELECT n + 1 
    FROM Numbers 
    WHERE n+1 <= 10 
), 
se7en AS 
(
    SELECT n = 1 
    UNION ALL 
    SELECT n + 1 
    FROM se7en 
    WHERE n+1 <= 7 
) 
SELECT Numbers.n 
FROM Numbers CROSS JOIN se7en 
1

无需使用recursive CTE这个你可以尝试设置为基础的方法解决方案尝试这样的事情。种类integer师。

如果您有权访问主数据库,请使用此选项。

;with cte as 
(
SELECT top 1000 [7_seq] = ((Row_number()OVER(ORDER BY (SELECT NULL)) - 1)/7) + 1 
FROM sys.columns 
) 
select * from cte where [7_seq] <= @n 

或使用tally table来产生数字。我将在下面更喜欢这种解决方案

DECLARE @n INT = 10; 

WITH Tally (num) 
    AS (
     -- 1000 rows 
     SELECT Row_number()OVER (ORDER BY (SELECT NULL)) 
     FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n) 
       CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n) 
       CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)), 
    seq 
    AS (SELECT [7_seq] = ((Row_number() 
           OVER(
            ORDER BY (SELECT num)) - 1)/7) + 1 
     FROM Tally) 
SELECT [7_seq] 
FROM seq 
WHERE [7_seq] <= @n 
1

你可以做到这一点:

DECLARE @num INT = 1, 
     @sub INT = 0, 
     @max INT = 10,   
     @timesToRepeat INT = 7 

CREATE TABLE #Temp (num INT) 


WHILE (@num < @max + 1) 
BEGIN 
    SET @sub = 0; 
    WHILE (@sub < @timesToRepeat) 
    BEGIN 
     INSERT INTO #Temp 
     SELECT @num x 
     SET @sub = @sub +1 
    END 
    SET @num = @num +1 

END 

SELECT * FROM #Temp 

DROP TABLE #Temp 

设置@max变量人数要达到它现在是10所以它会返回结果集像什么:

1 
1 
1 
1 
1 
1 
1 
2 
2 
2 
2 
2 
2 
2 
. 
. 
. 
10 
10 
10 
10 
10 
10 
10 
1
with x as 
(select 1 as id 
union all 
select 2 as id 
union all 
select 3 as id 
union all 
select 4 as id 
union all 
select 5 as id 
union all 
select 6 as id 
union all 
select 7 as id) 
select x1.* from x cross join x x1 

交叉联接将工作你的情况。

1
WITH t1 AS (SELECT 0 as num UNION ALL SELECT 0) 
    ,t2 AS (SELECT 0 as num FROM t1 as a CROSS JOIN t1 as b) 
    ,t3 AS (SELECT 0 as num FROM t2 as a CROSS JOIN t2 as b) 
    ,t4 AS (SELECT 0 as num FROM t3 as a CROSS JOIN t3 as b) 
    ,Tally (number) 
    AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) FROM t4) 
SELECT t1.number 
FROM Tally as t1 cross join Tally as t2 
where t2.number <=7 
ORDER BY t1.number; 
0
DECLARE @MAX INTEGER 
SET @MAX = 5; 
with cte as 
(SELECT 7 as num 
UNION ALL 
SELECT num-1 as num from cte where num>1 
),cte2 AS 
(SELECT @MAX as num 
UNION ALL 
SELECT num-1 as num from cte2 where num>1) 
select C2.num from cte C1,cte2 C2 ORDER by C2.num asc 

变化@MAX的值,以反映的n

0

值这里有一个稍微不同的方式来做到这一点。

select null num into #a 
union all 
select null 
union all 
select null 
union all 
select null 
union all 
select null 
union all 
select null 
union all 
select null 

select * into #b from 
    (select rn = row_number()over (order by (select null)) from sys.objects A cross join sys.objects B) A 
where rn <=10 

select #b.rn as numbers from #a cross join #b 
order by 1