The table structure of the database is like this
Then you need to get the query result like this
The sql statement is like this
select name,
'b1' as b1, sum(case when bag = 'b1' then money end),
'b2' as b2, sum(case when bag = 'b2' then money end),
'sum' as summ,sum(money)
from testtable
group by name;