liuyxit
发贴: 13
积分: 0
|
于 2005-03-04 15:32
SQLserver try this: /* Cost Center Month Cost -------------------------------------------------- 00001 01 90000.00 00001 02 80000.00 00001 03 30000.00 00001 04 90000.00 00002 01 10000.00 00002 02 20000.00 00002 03 30000.00 00002 04 10000.00
auther: LiuYXit */ create table #t (CostConter char(5),Month char(2),Cost numeric(10,2) )
insert into #t select '00001', '01',90000.00 union all select '00001', '02',80000.00 union all select '00001', '03',30000.00 union all select '00001', '04',90000.00 union all select '00002', '01',10000.00 union all select '00002', '02',20000.00 union all select '00002', '03',30000.00 union all select '00002', '04',10000.00
declare @sql varchar(1000) SET @sql = ''
SELECT @sql = @sql + ',SUM(CASE Month when ''' + Month + ''' THEN Cost END) AS [' + Month+']' FROM #t group by Month
EXEC ('SELECT CostConter ' + @sql + ' FROM #t GROUP BY CostConter ')
drop table #t
|