日期:2014-05-19  浏览次数:20473 次

请问怎么把多行的数据合并到一行里去啊?
我现在有一个表:opus  
现在有以下数据:
opus表:
type                 name
武侠                 射雕
武侠                 七剑
武侠                 神雕
诗词                 乐府
诗词                 海子

现在我要得到以下结果:
type     name1       name2     name3  
武侠:射雕         七剑       神雕
诗词:乐府         海子
请问该怎么办啊??。。。。。。。。。。比较着急




------解决方案--------------------
我只想出了这个写死的,动态的还没想出来

create table #temp
(type varchar(50),
name varchar(50)
)
insert into #temp
select '武侠 ', '射雕 ' union all
select '武侠 ', '七剑 ' union all
select '武侠 ', '神雕 ' union all
select '诗词 ', '乐府 ' union all
select '诗词 ', '海子 '
select * from #temp

select type,
max(case [id] when 1 then name when 4 then name else ' ' end) name1,
max(case [id] when 2 then name when 5 then name else ' ' end) name2,
max(case [id] when 3 then name else ' ' end) name3
from #temp
group by type
order by type DESC

------------
type name1 name2 name3
武侠 射雕 七剑 神雕
诗词 乐府 海子