抱歉了还是这个题目,但有点变化,三等奖可以是两个数字,需要把table1的两个三等奖合并到table2里面
现有的table1:
总共抽奖四次抽奖号码 抽奖日期 奖分类
-------------------------------
0000 2010-12-15 一等奖;数字0,两次出现在抽奖结果中
0000 2010-12-16 三等奖;
0000 2010-12-18 三等奖;
1111 2010-12-15 二等奖;数字1,三次出现在抽奖结果中
1111 2010-12-16 一等奖
1111 2010-12-1 一等奖
333 2010-12-17 一等奖
2222 2010-12-16 二等奖;2也出现三次
2222 2010-12-17 二等奖
2222 2010-12-18 三等奖
3333 2010-12-16 三等奖;3出现两次
3333 2010-12-18 一等奖;
4444 2010-12-17 三等奖;4,5,7都只出现一次
5555 2010-12-18 二等奖
5555 2010-12-18 三等奖
7777 2010-12-15 三等奖
7777 2010-12-17 三等奖
想把上表转换成如下表格:
抽奖日期 一等奖 二等奖 三等奖
2010-12-15 0000 1111 7777 5555
2010-12-16 1111 2222 3333 0000
2010-12-17 1111 2222 4444 7777
2010-12-18 3333 5555 2222 0000谢谢
现有的table1:
总共抽奖四次抽奖号码 抽奖日期 奖分类
-------------------------------
0000 2010-12-15 一等奖;数字0,两次出现在抽奖结果中
0000 2010-12-16 三等奖;
0000 2010-12-18 三等奖;
1111 2010-12-15 二等奖;数字1,三次出现在抽奖结果中
1111 2010-12-16 一等奖
1111 2010-12-1 一等奖
333 2010-12-17 一等奖
2222 2010-12-16 二等奖;2也出现三次
2222 2010-12-17 二等奖
2222 2010-12-18 三等奖
3333 2010-12-16 三等奖;3出现两次
3333 2010-12-18 一等奖;
4444 2010-12-17 三等奖;4,5,7都只出现一次
5555 2010-12-18 二等奖
5555 2010-12-18 三等奖
7777 2010-12-15 三等奖
7777 2010-12-17 三等奖
想把上表转换成如下表格:
抽奖日期 一等奖 二等奖 三等奖
2010-12-15 0000 1111 7777 5555
2010-12-16 1111 2222 3333 0000
2010-12-17 1111 2222 4444 7777
2010-12-18 3333 5555 2222 0000谢谢
select '1111','2010-12-15','二等奖' union all
select '1111','2010-12-16','一等奖' union all
select '1111','2010-12-17','一等奖' union all
select '2222','2010-12-16','二等奖' union all
select '2222','2010-12-17','二等奖' union all
select '2222','2010-12-18','三等奖' union all
select '3333','2010-12-16','三等奖' union all
select '3333','2010-12-18','三等奖' union all
select '4444','2010-12-17','三等奖' select 抽奖日期,一等奖=stuff((select ','+抽奖号码 from tb where a.抽奖日期=抽奖日期 and 奖分类='一等奖' for xml path('')),1,1,''),
二等奖=stuff((select ','+抽奖号码 from tb where a.抽奖日期=抽奖日期 and 奖分类='二等奖' for xml path('')),1,1,''),
三等奖=stuff((select ','+抽奖号码 from tb where a.抽奖日期=抽奖日期 and 奖分类='三等奖' for xml path('')),1,1,'')
from tb a group by 抽奖日期/*
抽奖日期 一等奖 二等奖 三等奖
----------------------- -----------
2010-12-15 00:00:00.000 0000 1111 NULL
2010-12-16 00:00:00.000 1111 2222 3333
2010-12-17 00:00:00.000 1111 2222 4444
2010-12-18 00:00:00.000 NULL NULL 2222,3333
2010-12-15 00:00:00.000 0000,1111 0000,1111 0000,1111
2010-12-16 00:00:00.000 1111,2222,3333 1111,2222,3333 1111,2222,3333
2010-12-17 00:00:00.000 1111,2222,4444 1111,2222,4444 1111,2222,4444
2010-12-18 00:00:00.000 2222,3333 2222,3333 2222,3333
declare @table table (抽奖号码 varchar(4),抽奖日期 datetime,奖分类 varchar(20))
insert into @table
select '0000','2010-12-15','一等奖' union all
select '1111','2010-12-15','二等奖' union all
select '1111','2010-12-16','一等奖' union all
select '1111','2010-12-17','一等奖' union all
select '2222','2010-12-16','二等奖' union all
select '2222','2010-12-17','二等奖' union all
select '2222','2010-12-18','三等奖' union all
select '3333','2010-12-16','三等奖' union all
select '3333','2010-12-18','三等奖' union all
select '4444','2010-12-17','三等奖' ;WITH maco AS
(
select * from(select distinct 抽奖日期,奖分类 from @table)a outer apply(
select 抽奖号码= stuff(replace(replace(
(
select 抽奖号码 from @table n
where 抽奖日期 = a.抽奖日期 AND 奖分类=a.奖分类
for xml auto
), '<n 抽奖号码="',','), '"/>', ''), 1, 1, '')
)N
)select
[抽奖日期]=CONVERT(VARCHAR(10),抽奖日期,120),
[一等奖]=max(CASE 奖分类 WHEN '一等奖' THEN 抽奖号码 ELSE '' END),
[二等奖]=max(CASE 奖分类 WHEN '二等奖' THEN 抽奖号码 ELSE '' END),
[三等奖]=max(CASE 奖分类 WHEN '三等奖' THEN 抽奖号码 ELSE '' END)
from maco GROUP BY 抽奖日期
Thanks a lot.