我想得到两栏之间的区别,这是我当前的查询:
select customer_id as customers_id, sum(amount) as manual_amount,
(select amount from customers where id = customers_id) as current_wallet_amount
FROM wallet_transactions
where customer_id = 14438
group by wallet_transactions.customer_id
我得到的结果附在下面:
我需要的是人工金额和current_wallet_amount之差。任何帮助都是非常可贵的
SELECT
wt.customer_id AS customers_id,
SUM(wt.amount) AS manual_amount,
ANY_VALUE(customers.amount) AS current_wallet_amount,
(SUM(wt.amount) - ANY_VALUE(customers.amount)) AS diff_amount
FROM wallet_transactions AS wt
JOIN customers ON customers.id = wt.customer_id
WHERE customer_id = 14438
GROUP BY wt.customer_id
您可以在下面尝试-
SELECT cu.id AS customers_id,
SUM(wt.amount) AS manual_amount,
cu.amount AS current_wallet_amount,
SUM(wt.amount) - cu.amount AS difference
FROM wallet_transactions AS wt
JOIN customers cu ON cu.id = wt.customer_id
WHERE cu.id = 14438
GROUP BY cu.id, cu.amount