就是写着玩,假设存在这么一张表 test,表中有两个字段currency_code(币种编码'HKD', 'USD')、amount(金额),
现在有这么一个需求,要求分别统计各币种对应的金额,怎么办,可以使用这种sql句法"case when then else end"。
具体写法:
select sum(case when currency_code='HKD' then amount else 0 end) as 'hkd_amount',
sum(case when currency_code='USD' then amount else 0 end) as 'usd_amount'
from test;