我确实有merchant_credit_history表,我在其中存储商家信用、借记卡和交易类型。
我想根据交易类型对贷方或借方求和。
记录
我尝试过这个:
选择 SUM(ABS(借方)) 作为 dealer_credit 来自merchant_credits_history WHERE transaction_type='premium_debit' 和merchant_id='x'
联盟
选择 SUM(ABS(credit)) 作为 dealer_margin_earned 来自merchant_credits_history WHERE transaction_type='margin_credit' 和merchant_id='x'
输出为
如果我能有更好的查询以及单独的键值对中的值,我将不胜感激
最佳答案
SELECT
SUM(CASE(WHEN transaction_type='premium_debit') THEN debit ELSE 0)) as dealer_credit,
SUM(CASE(WHEN transaction_type='margin_credit') THEN credit ELSE 0)) as dealer_margin_earned
FROM merchant_credits_history
WHERE merchant_id='x';
因此,您可以获得每个merchant_id 的一对贷方、借方值。
关于Mysql根据多案例取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39806429/