根据货币计算 SUM

chr*_*con 0 mysql sum

我需要SUM从下面的查询中获取所有货币值的总计,现在我得到 3 个(或 4 个 - 取决于数据库中的货币)结果,但只需要总计。

请看这个sqlFiddle

查询是:

select 
case when currency = 'GBP' then 
    sum(itemPrice) + sum(shippingPrice) * 1.33
else
    case when currency = 'USD' then
    sum(itemPrice) + sum(shippingPrice) * 0.92
else 
    case when currency = 'CAD' then 
    sum(itemPrice) + sum(shippingPrice) * 0.65
else 
    case when currency = 'MXN' then
    sum(itemPrice) + sum(shippingPrice) * 0.0513 
else sum(itemPrice) + sum(shippingPrice)
end end end end
as turnover, currency from foo
group by currency
Run Code Online (Sandbox Code Playgroud)

当我尝试将所有箱子包裹在另一个箱子中时,sum()我得到了

群组功能使用不当

我该如何做到最好?

Bar*_*mar 5

摆脱掉GROUP BY,做SUM 外面的事 CASE

SELECT SUM((itemPrice + shippingPrice) *
        CASE currency
            WHEN 'GBP' THEN 1.33
            WHEN 'USD' THEN 0.92
            WHEN 'CAD' THEN 0.65
            WHEN 'MXN' THEN 0.0513
            ELSE 1.0
        END) AS turnover
FROM foo
Run Code Online (Sandbox Code Playgroud)