如果类型固定的话:
select a.id,rmb,hkd from (select id,sum(num) as rmb from table1 where type='rmb' group by id) a,
(select id,sum(num) as hkd from table1 where type='hkd' group by id) b
where a.id=b=id
--如果type的个数固定,可以不用上面的动态sql
create table tb(ID varchar(10),Type varchar(10),Num int)
insert into tb select '001','RMB',500
union all select '002','KHD',300
union all select '001','KHD',450
union all select '003','RMB',200
union all select '001','RMB',100
union all select '002','KHD',600
select ID,
RMB=sum(case Type when 'RMB' then Num else 0 end),
KHD=sum(case Type when 'KHD' then Num else 0 end)
from tb
group by ID
create table tb(ID varchar(10),Type varchar(10),Num int)
insert into tb select '001','RMB',500
union all select '002','KHD',300
union all select '001','KHD',450
union all select '003','RMB',200
union all select '001','RMB',100
union all select '002','KHD',600
declare @sql varchar(8000)
set @sql='select ID'
select @sql=@sql+',['+Type+']=sum(case Type when '''+Type+''' then Num else 0 end)' from tb group by Type order by type desc
exec(@sql+' from tb group by ID')