交叉表1

/*SELECT s.id,s.name,s.tid,t.typename,c.counts,c.time into #abc
FROM foods as s
INNER JOIN
foodtype as t
ON s.tid = t.id
INNER JOIN
foodcount as c
ON s.id=c.fid*/
drop table #abc

declare @sql varchar(8000)
set @sql=''
select @sql=@sql+ ',''' + Convert(varchar(10),[time],120) + '''=isnull(sum(case Convert(varchar(10),[time],120) when ''' +Convert(varchar(10),[time],120)+''' then [counts] end),0 )'
from (select distinct [time] from foodcount) a

print @sql
exec ('select typename'+@sql+' from #abc group by typename')

posted @ 2009-01-20 12:02  Dacey  Views(119)  Comments(0Edit  收藏  举报