聚合12个月

lingshao0909 2008-09-04 03:28:01
有一个人员记录表,想形成一个新表统计一年的数据。新表是每个人都有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  男
...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingshao0909 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 libin_ftsafe 的回复:]
SQL codeselect a.id,b.yue,a.name,a.sex from 人员表 a,(select 1 as yue union select 2 union ... union select 12) b
[/Quote]
完全行得通,老大真猛,不愧是钻石啊!!
gahade 2008-09-04
  • 打赏
  • 举报
回复
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
子陌红尘 2008-09-04
  • 打赏
  • 举报
回复

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 新表
子陌红尘 2008-09-04
  • 打赏
  • 举报
回复
select a.id,b.yue,a.name,a.sex from 人员表 a,(select 1 as yue union select 2 union ... union select 12) b
天-笑 2008-09-04
  • 打赏
  • 举报
回复
declare @month table
(ID int )

DECLARE @ID INT
SET @ID = 1
WHILE @ID<13
BEGIN
insert into @month
select @id
SET @id = @id + 1
END

declare @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

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧