出于某种原因,以下查询仅返回第一个查询的总和(金额),而不是第一个总和(金额)与第二个总和(金额)之间的预期差。
select sum(amount) from(
SELECT
UNIQUE_MEM_ID ,
Amount
FROM yi_fourmpanel.card_panel
WHERE (DESCRIPTION iLIKE '%some criteria%')
GROUP BY
UNIQUE_MEM_ID ,
Amount)
minus
select sum(amount) from(
SELECT
UNIQUE_MEM_ID ,
Amount
FROM yi_fourmpanel.card_panel
WHERE (DESCRIPTION iLIKE '%some criteria%')
AND is_duplicate!=1
AND amount >0
AND currency_id = 152
AND transaction_base_type = 'debit'
GROUP BY
UNIQUE_MEM_ID ,
Amount)
答案 0 :(得分:0)
MINUS/EXCEPT
是基于运算符而非算术运算符设置的。我想你想要类似的东西:
WITH cte AS (
SELECT UNIQUE_MEM_ID , Amount
FROM yi_fourmpanel.card_panel
WHERE (DESCRIPTION iLIKE '%some criteria%')
AND transaction_base_type <> 'debit'
UNION ALL
SELECT UNIQUE_MEM_ID , -Amount
FROM yi_fourmpanel.card_panel
WHERE (DESCRIPTION iLIKE '%some criteria%')
AND is_duplicate!=1
AND amount >0
AND currency_id = 152
AND transaction_base_type = 'debit'
)
SELECT UNIQUE_MEM_ID, SUM(Amount) AS total
FROM cte
GROUP BY UNIQUE_MEM_ID