基于字段值的乘法重复行

时间:2013-03-26 10:38:07

标签: sql sql-server-2008 repeat

我正在尝试根据表格列中的值生成多行。我需要的行数是列中数据值的三倍。目前我有它,所以它根据列中的值重复行数,但我无法弄清楚如何使其在列中获得三倍的值。 这是我的代码:

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);

以下是我的结果示例: 所以这里显示了Lucy Adams的3行,但是我需要它来显示9行

由于

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

作为一个注释,有一种替代方法,通常不需要扫描表三次:

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