日期:2014-05-17  浏览次数:21025 次

请教高手,在线等。行转列。
现有格式:
Name year value
A 2009 1
A 2010 2
A 2011 3
B 2009 11
B 2010 22
B 2010 33 

转化为
Name 2009 2010 2011
A 1 2 3
B 11 22 33 


怎么做?请教高手了。 可以用Linq 也可以用代码。最好是用LInq。

另外主要问题是 2009 2010 2011 是不定的。 是会变化的。所以不能写死。
所以要做成动态的。



------解决方案--------------------
http://topic.csdn.net/u/20110620/15/bec4adee-ebd6-40c7-88f6-250050f79680.html

看这个帖子中我的回复。
------解决方案--------------------
SQL Server中实现
http://blog.csdn.net/taomanman/article/details/6684013
------解决方案--------------------
http://topic.csdn.net/u/20110318/14/5f7f3f4e-05a8-41f1-9116-a5813a758af2.html
------解决方案--------------------
C# 实现DataTable的行转列 .

http://blog.csdn.net/sandy945/article/details/5336560

DataList 实现 行转列 .

http://blog.csdn.net/sandy945/article/details/4068760
------解决方案--------------------
sql可以不?
SQL code
create table #temp(Name varchar(20),year varchar(20),value varchar(20))
insert into #temp
select 'A','2009','1' union all
select 'A','2010','2' union all
select 'A','2011','3' union all
select 'B','2009','11' union all
select 'B','2010','22' union all
select 'B','2011','33'


declare @sql varchar(500)
set @sql = 'select Name'
select @sql=@sql+',max(case year when '''+[year]+''' then value end) ['+[year]+']'
from (select distinct [year] from #temp) as a
set @sql = @sql +' from #temp group by Name'
exec(@sql)

------解决方案--------------------
探讨
加一句对Name列的排序 该加到哪里咯? 一楼高手。数据已经出来了。