Try this(you can also do it by SELF JOIN):
;WITH D AS
(
SELECT ACNUMBER, SUM(TRANSACTION) AS Deposit
FROM TABLE_NAME
WHERE TRANSACTION_TYPE = 'Deposit'
GROUP BY ACNUMBER
)
, W as
(
SELECT ACNUMBER, SUM(TRANSACTION) AS Withdrawal
FROM TABLE_NAME
WHERE TRANSACTION_TYPE = 'Withdrawal'
GROUP BY ACNUMBER
)
SELECT D.ACNUMBER, d.Deposit, w.Withdrawal
FROM D
INNER JOIN W
ON D.ACNUMBER = W.ACNUMBER
WHERE d.Deposit < w.Withdrawal
You should not group by transaction type. Try this
select acnumber,sum(transaction_amount * case when transaction_type = 'deposit' then 1 else -1 end) as amount
from trandetails
group by acnumber
order by acnumber asc
You may be interested only in rows where the amount is negative as these are the accounts where withdrawl > deposit. In that case add a filter:
select acnumber,sum(transaction_amount * case when transaction_type = 'deposit' then 1 else -1 end) as amount
from trandetails
group by acnumber
having sum(transaction_amount * case when transaction_type = 'deposit' then 1 else -1 end) < 0
order by acnumber asc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.