求SQL语句
请问如何把ID strDate
1 2010-10-27
1 2010-10-28
1 2010-10-29
2 2010-10-27
2 2010-10-28
2 2010-10-29
5 2010-10-27
5 2010-10-28
5 2010-10-29
. .
. .
. .
变成如下:
1 2010-10-27 2010-10-28 2010-10-29 ...
2 2010-10-27 2010-10-28 2010-10-29 ...
5 2010-10-27 2010-10-28 2010-10-29 ...
用SQL语句编写出来 --------------------编程问答-------------------- 这个好像不行,关注中,帮顶。 --------------------编程问答-------------------- 请自行搜索行转列,列转行 --------------------编程问答-------------------- 一条sql是不可以的吧,就要想其他办法了。 --------------------编程问答--------------------
declare @i int, @count int, @sql nvarchar(4000), @sqlsub nvarchar(4000)--------------------编程问答-------------------- http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html?seed=206485877&r=61545678#r_61545678
select @i = 1, @count = max(c) from ( select sum(1) as c from 表 group by ID) as a
set @sqlsub = ''
select @sqlsub = @sqlsub + ' select row_number() over(order by id) as idx, ID, [strDate] from 表 where ID = ''' + Convert(varchar(10),ID) + ''' union all'
from (select distinct ID from 表) as b
set @sqlsub = substring(@sqlsub, 0, len(@sqlsub) - 9)
set @sql = 'select ID '
while @i <= @count begin
set @sql = @sql + ', max(case idx when ' + cast(@i as nvarchar(10)) + ' then [strDate] else null end ) as [第' + cast(@i as nvarchar(10)) + '次] '
set @i = @i + 1
end
set @sql = @sql + ' from (' + @sqlsub + ')t group by ID'
print @sqlsub
exec(@sql)
补充:.NET技术 , C#