test

0

Please login or register to vote for this query.

(click on this box to dismiss)

The Workplace Meta

Q&A about the site for members of the workforce navigating the professional setting

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 transaction_id,wallet_id, (tot_in-tot_out) as balance
        from (Select * , sum(money_in) over(PARTITION  by wallet_id order by transaction_id) as tot_in 
                       , sum(money_out) over(PARTITION  by wallet_id order by transaction_id) as tot_out 
                       from @transactions
                       ) as subq
where tot_in - tot_out < 0;

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results