直接例子:表A(itemid,itemdate,xh)
itemid,itemdate,xh
a,2010-3-10,null
b,2010-3-15,null
c,2010-3-20,null
b,2011-4-14,null
a,2011-4-15,null
c,2010-4-30,null
b,2011-5-17,null需要更新的结果是:
a,2010-3-10,1
a,2011-4-15,2
b,2010-3-15,1
b,2011-4-14,2
b,2011-5-17,3
c,2010-3-20,1
c,2010-4-30,2最后的展现的结果是:
itemid\xh→, 1 ,2 ,3 ,4.....
a,2010-3-10,2010-4-15,
b,2010-3-15,2010-4-14,2010-5-17
c,2010-3-20,2010-4-30,
itemid,itemdate,xh
a,2010-3-10,null
b,2010-3-15,null
c,2010-3-20,null
b,2011-4-14,null
a,2011-4-15,null
c,2010-4-30,null
b,2011-5-17,null需要更新的结果是:
a,2010-3-10,1
a,2011-4-15,2
b,2010-3-15,1
b,2011-4-14,2
b,2011-5-17,3
c,2010-3-20,1
c,2010-4-30,2最后的展现的结果是:
itemid\xh→, 1 ,2 ,3 ,4.....
a,2010-3-10,2010-4-15,
b,2010-3-15,2010-4-14,2010-5-17
c,2010-3-20,2010-4-30,
drop table #temp
go
create table #temp(itemid char(1), itemdate varchar(10), xh int)
insert #temp(itemid, itemdate)
select 'a', '2010-3-10' union all
select 'b', '2010-3-15' union all
select 'c', '2010-3-20' union all
select 'b', '2011-4-14' union all
select 'a', '2011-4-15' union all
select 'c', '2010-4-30' union all
select 'b', '2011-5-17'
go
--SQL:
declare @fieldList varchar(max)
set @fieldList = ''
SELECT @fieldList=@fieldList+','+QUOTENAME(LTRIM(ROW_NUMBER() over(order by getdate())))
from #temp
where itemid = (select top(1) itemid from #temp group by itemid order by COUNT(*) desc)
set @fieldList = STUFF(@fieldList, 1, 1, '')declare @sql varchar(max)
set @sql='
select [itemid\xh→] = itemid, '+@fieldList+' from
(SELECT rowno=ROW_NUMBER() over(partition by itemid order by itemdate),itemid, itemdate FROM #TEMP) a
pivot
(max(itemdate) for rowno in('+@fieldList+')) b
'exec(@sql)
/*
itemid\xh→ 1 2 3
a 2010-3-10 2011-4-15 NULL
b 2010-3-15 2011-4-14 2011-5-17
c 2010-3-20 2010-4-30 NULL
*/
drop table #temp
go
create table #temp(itemid char(1), itemdate varchar(10), xh int)
insert #temp(itemid, itemdate)
select 'a', '2010-3-10' union all
select 'b', '2010-3-15' union all
select 'c', '2010-3-20' union all
select 'b', '2011-4-14' union all
select 'a', '2011-4-15' union all
select 'c', '2010-4-30' union all
select 'b', '2011-5-17'
go
--SQL:
declare @fieldList varchar(max)
set @fieldList = ''
SELECT @fieldList=@fieldList+','+(LTRIM(ROW_NUMBER() over(order by getdate())))
from #temp
where itemid = (select top(1) itemid from #temp group by itemid order by COUNT(*) desc)
set @fieldList = STUFF(@fieldList, 1, 1, '')declare @sql varchar(max)
set @sql='
select [itemid\xh→,'+ @fieldList +'] =
itemid + '','' + STUFF((select itemdate+'','' from #temp where itemid=T.itemid order by itemdate for xml path('''')),1,1,'''')
from #temp T
group by itemid
'
exec(@sql)
/*
itemid\xh→,1,2,3
a,010-3-10,2011-4-15,
b,010-3-15,2011-4-14,2011-5-17,
c,010-3-20,2010-4-30,
*/
from 表A a
select [itemid\xh→] = itemid, [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13] from
(SELECT rowno=ROW_NUMBER() over(partition by itemid order by itemdate),itemid, itemdate FROM #TEMP) a
pivot
(max(itemdate) for rowno in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13])) b
/*
itemid\xh→ 1 2 3 4 5 6 7 8 9 10 11 12 13
a 2010-3-10 2011-4-15 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
b 2010-3-15 2011-4-14 2011-5-17 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
c 2010-3-20 2010-4-30 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
*/