朋友们,我有一张金额和税金的表格如下
表1
Amount tax
200 5
300 2
100 12
50 5
200 12
在上表中,我有amount
列和tax
,这里我想要与tax
子句的总金额
例如sum(amount) where tax='2'
,sum(amount) where tax='5'
和sum(amount) where tax='12'
我希望输出像
amount_2 | tax_2 | amount_5 | tax_5 | amount_12 | tax_12
----------------------------------------------------------
300 | 2 | 250 | 5 | 300 | 12
答案 0 :(得分:1)
您可以为条件和
编写如下内容select
sum(case when tax = 2 then amount else 0 end) amount_2,
2 as tax_2,
sum(case when tax = 5 then amount else 0 end) amount_5,
5 as tax_5,
sum(case when tax = 12 then amount else 0 end) amount_12,
12 as tax_12
from table
/* group by somecol if needed sum per group*/