我正在尝试创建一个字段
1.sum购物字段,其中购买的价值类型=增值税为purchase_vat
2.sum购买字段,其中购买类型的值=免除为purchase_exempt
源表&附加图像中的结果表示例
答案 0 :(得分:1)
这个怎么样?
SUM(CASE WHEN purchases_type='vat' THEN Purchases ELSE 0 END) AS purchases_vat,
SUM(CASE WHEN purchases_type='exempt' THEN Purchases ELSE 0 END) AS purchases_exempt,
答案 1 :(得分:0)
两行版本:
SELECT 'vat', SUM(purchases)
FROM source_table
WHERE purchase_type = 'vat4'
UNION
SELECT 'exempt', SUM(purchases)
FROM source_table
WHERE purchase_type = 'exempt';
一行:
SELECT
(SELECT sum(purchases) from source_table where purchase_type = 'vat4') purchase_vat,
(SELECT sum(purchases) from source_table where purchase_type = 'exempt') purchase_exempt;
答案 2 :(得分:0)
这将为您提供“增值税”的购买总和。您可以类似地为'豁免'
执行此操作SELECT `s` FROM (SELECT SUM(purchases), type FROM my_table GROUP BY type) as T1 WHERE T1.type = 'vat
答案 3 :(得分:0)
select
sum(case when purchase_type = 'vat4' then purchases else 0 end) as purchase_vat4,
sum(case when purchase_type = 'exempt' then purchases else 0 end) as purchase_exempt
from source_table