try this:create table deliver(customerid int,quantity int,type varchar(20))insert into deliverselect 1,10,'delivery'union all select 2,5,'delivery'union all select 1,5,'return'union all select 2,1,'return'union all select 3,10,'delivery'union all select 4,10,'delivery'union all select 3,5,'return'union all select 1,5,'delivery'select d.customerID,isnull((select sum(d1.quantity) from deliver as d1 where d1.customerid = d.customerID and type = 'delivery'),0) as Delivery,isnull((select sum(d1.quantity) from deliver as d1 where d1.customerid = d.customerID and type = 'return'),0) as [Return] from deliver as d group by d.customerID order by d.customerID