有一个人员记录表,想形成一个新表统计一年的数据。新表是每个人都有12个月的行
例如:
人员表
id name sex
1  aa  男
2  bb  男
3  cc  女
4  dd  女
5  ee  女
6  ff  男
新表
id yue name sex
1  1  aa  男
1  2  aa  男
1  3  aa  男
1  4  aa  男
1  5  aa  男
1  6  aa  男
1  7  aa  男
1  8  aa  男
1  9  aa  男
1  10  aa  男
1  11  aa  男
1  12  aa  男

解决方案 »

  1.   

    declare @month table
    (ID int )DECLARE @ID INT
    SET @ID = 1
    WHILE @ID<13
    BEGIN
    insert into @month
    select @id
    SET @id = @id + 1
    ENDdeclare  @p table
    (id  int,
    name varchar(10),
    sex  varchar(10))
    insert into @p
    select 1,'aa','男'
    union all
    select 2,'bb','女'select * from @month cross join @p
      

  2.   

    select a.id,b.yue,a.name,a.sex from 人员表 a,(select 1 as yue union select 2 union ... union select 12) b
      

  3.   


    select a.id,b.yue,a.name,a.sex into 新表 from 人员表 a,(select 1 as yue union select 2 union ... union select 12) b select * from 新表
      

  4.   

    create table 人员表(id int,name varchar(10),sex char(2))
    insert into 人员表
    select 1,'aa','男'
    union all select 2,'bb','男'
    union all select 3,'cc','女'
    union all select 4,'dd','女'
    union all select 5,'ee','女'
    union all select 6,'ff','男'
    select *
    from 人员表,
    (select 1 as 'yue'
    union select 2 
    union select 3 
    union select 4 
    union select 5
    union select 6
    union select 7
    union select 8
    union select 9
    union select 10
    union select 11
    union select 12) t
    order by id,yue