2015-07-13 3 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