create table tbl (F_NM1 int,F_NM2 int,F_QTY int) insert tbl select 1,101,1000 union all select 1,102,2000 union all select 1,103,99 union all select 2,101,3000 union all select 2,102,4000 union all select 2,103,599 go declare @s varchar(8000) set @s='' select @s=@s+',[F_'+cast(F_NM2 as varchar) +']=sum(case F_NM2 when '+cast(F_NM2 as varchar) +' then F_QTY else 0 end)' from tbl group by F_NM2 exec('select F_NM1'+@s+' from tbl group by F_NM1') goHTH