假设我有
SELECT `Type`, `Originating Country Code`,`Originating Currency Code`, SUM(`Principal Amount`) as Total
FROM `colab`
WHERE (`Destination Country Code`='PH'
AND `Destination Currency Code`='PHP'
AND `Type` = 'PAID') || (`Destination Country Code`='PH'
AND `Destination Currency Code`='USD'
AND `Type`= 'PAID')
GROUP BY `Type`
工作正常并返回true请见下文
country_code | Currency_Code | Type | Principal
=======================================================
| PH | PHP | PAID | 200
=======================================================
我的问题是我想用美元价值分离货币总本金额。请查看我的数据库中的记录
country_code | Currency_Code | Type | Principal
=======================================================
| PH | PHP | PAID | 100
| PH | USD | PAID | 100
=======================================================
答案 0 :(得分:3)
尝试此查询:
SELECT `Currency_Code`, SUM(`Principal Amount`) as Total
FROM `table`
GROUP BY `Currency_Code`
答案 1 :(得分:1)
您可以像GROUP BY
这样使用:
SELECT `Currency_Code`, SUM(`Principal Amount`) as Total
FROM `table`
WHERE `Country Code`='PH'
AND `Type` LIKE '%W%'
GROUP BY `Currency_Code`
如果您想添加更多条件,只需使用WHERE
或AND
向OR
子句添加更多部分,例如:
SELECT `Currency_Code`, SUM(`Principal Amount`) as Total
FROM `table`
WHERE `Country Code`='PH'
AND `Type` LIKE '%W%'
AND `Currency_Code` IN('USD', 'CAD', 'YEN')
AND `Principal_Amount` > 10
GROUP BY `Currency_Code`
答案 2 :(得分:0)
您需要使用GROUP BY
即
SELECT `Currency Code`, SUM(`Principal Amount`) as Total
FROM `table`
WHERE `Country Code`='PH'
AND `Type` LIKE '%W%'
GROUP BY `Currency Code`
答案 3 :(得分:0)
你不能根据你的要求改变查询吗?
我的意思是从您的预期结果我推断您需要基于Type或country_code的结果,因此从查询中删除“AND Currency Code
='PHP'”将为您提供预期的结果。
答案 4 :(得分:0)
将您的条件分解为子查询并加入其中。只需添加任意数量的规则即可。
SELECT `Country Code`, `Currency Code`, `Type`,
SUM(`Principal Amount`) as Total
FROM `table`
JOIN (
SELECT 'PH' as a, 'PHP' as b, '%W%' as c
UNION SELECT 'PH', 'USD', '%W%'
UNION SELECT 'PY', 'PYC', '%H%'
) AS criteria
ON (`table`.`Country Code` = criteria.a and
`table`.`Currency Code` = criteria.b and
`table`.`Type` like criteria.c)
GROUP BY `Country Code`, `Currency Code`, `Type`