Q&A for those involved in the field of teaching Computer Science
DECLARE @transactions TABLE(transaction_id INT, customer_id INT, wallet_id INT, money_in INT, money_out INT); INSERT INTO @transactions VALUES (1, 1, 1, 10, 0), (2, 1, 1, 10, 0), (3, 1, 1, 0, 3), (4, 1, 2, 3, 0), (5, 1, 2, 0, 1), (6, 1, 2, 0, 3), (7, 1, 2, 0, 1), (8, 2, 3, 12, 0), (9, 2, 3, 5, 0), (10, 2, 3, 0, 9), (11, 2, 3, 0, 6), (12, 3, 4,100, 0), (13, 3, 4, 0, 40), (14, 3, 4, 0, 20), (15, 3, 4, 0, 40); --SELECT * from transactions; Select *, sum(money_in) over(PARTITION by customer_id, wallet_id) as tot_in , sum(money_out) over(PARTITION by customer_id, wallet_id) as tot_out from @transactions