SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Hint: use Ctrl+Enter for SQL autocomplete -- Hint: use Ctrl+Enter for SQL autocomplete DROP TABLE IF EXISTS my_table; CREATE TABLE my_table (transaction_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,client_id INT NOT NULL ,action_type VARCHAR(12) NOT NULL ,action_amount INT NOT NULL ); INSERT INTO my_table(client_id,action_type,action_amount) VALUES (1 ,'credit', 1000), (1 ,'debit', 100), (1 ,'credit', 500), (1 ,'debit', 300), (2 ,'debit', 1000), (2 ,'credit', 1200), (3 ,'debit', 1000), (3 ,'credit', 1000), (4 ,'debit', 1000); SET @csum =0; select * FROM( SELECT transaction_id, client_id , IF(action_type="credit",action_amount, 0) as CrAmt, IF(action_type<>"credit",action_amount, 0) as DrAmt, (@csum := @csum + IF(action_type="credit", action_amount, (action_amount*-1)))as balance FROM `my_table` WHERE client_id = '1' ORDER BY `my_table`.`transaction_id`) as cTbl order by transaction_id DESC
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear