2013-03-26 71 views
0

我試圖根據我的表的列中的值生成多行。我需要的行數是列中數據值的三倍。目前我有它,所以它重複基於列中的值的行數,但我無法解決如何獲得它的三倍在列中的值。 這裏是我的代碼:基於字段值的乘法重複行

WITH tmp as (SELECT 1 as rn UNION ALL SELECT rn+1 from tmp WHERE rn<=1000) 

    SELECT 
    [SURNAME], 
    [NAME], 
    [AGE], 
    [PAYMENT DAYS PER/WK] 

    FROM 
    [PublishedReporting].[dbo].[Sheet1$] 
    LEFT JOIN (SELECT * FROM tmp WHERE rn<=50) as 
            rowgen on [PAYMENT DAYS PER/WK] >= rowgen.rn 

    WHERE 
    [PAYMENT DAYS PER/WK] IS NOT NULL 

    ORDER BY 
    SURNAME, 
    NAME 

    option (maxrecursion 32767); 

這裏是我的結果的一個樣本: 所以這裏顯示3行露西·亞當斯,但我需要它來顯示9

感謝

回答

1
select * from [PublishedReporting].[dbo].[Sheet1$] 
union all 
select * from [PublishedReporting].[dbo].[Sheet1$] 
union all 
select * from [PublishedReporting].[dbo].[Sheet1$] 
order by 1; 
+0

感謝您的回答,但這產生了與我的查詢相同的結果 – Will 2013-03-26 10:51:55

+0

我得到它的工作使用您的答案變化,所以謝謝 – Will 2013-03-26 11:08:22

0

作爲一個說明,有一種替代方法,通常不需要三次掃描表格:

select t.* 
from [PublishedReporting].[dbo].[Sheet1$] t cross join 
     (select 1 as n union all select 2 union all select 3) rows3