Add and Substract one Row value to another row value in same table using mysql - mysql

Below is my table tbl_wallet. I want to add and subtract amount in same table addition and subtraction on the basis of credit and debit key
sno
w_userid
w_amount
w_type
1
263
100
credit
2
263
200
credit
3
263
100
credit
4
263
100
debit
Below is my query for getting detail
SELECT wl.w_userid
, wl.w_type
, wl.w_amount
, DATE_FORMAT(w_date,' %D %M %Y %r') AS w_date
, SUM(w_amount)
FROM tbl_wallet AS wl
WHERE wl.w_userid=263
ORDER BY wl.w_date DESC;
But i want the output like Below
sno
w_userid
w_amount
w_type
Balance
1
263
100
credit
100
2
263
200
credit
300
3
263
100
credit
400
4
263
100
debit
300

SELECT *,
SUM(CASE w_type WHEN 'credit' THEN w_amount
WHEN 'debit' THEN -w_amount
ELSE 0
END) OVER (PARTITION BY w_userid
ORDER BY sno ASC) AS Balance
FROM source_table

Related

How to get cumulative total for previous month and upto this month?

ID pcID contractor approver claimed
-------------------------------------------
1 1 one 1000 900
2 1 two 200 100
3 1 three 1000 1000
4 1 six 100 11
5 2 six 100 22
6 3 six 120 1
7 4 three 102 10
From the above table, I need to get cumulative amount for upto this month and previous month of approver and claimed and also current month approver, claimed amount based on the contractor. Like below table.
ID contractor approver claimed uptothisMTApprover uptothisMTClaimed previousMTApprover previousMTClaimed
-----------------------------------------------------------------------------------------------------------------
1 one 1000 900 1000 900 0 0
2 two 200 100 200 100 0 0
3 three 102 10 1102 1010 1000 1000
4 six 120 1 320 34 200 33
Thanks in advance..
You seem to want the latest row per contractor, as defined by pcID, and a cumulative sum of all previous months.
You can use window functions:
select contractor, approver, claimed,
total_approver as uptothisMTApprover,
total_claimed as uptothisMTClaimed,
total_approver - approver as previousMTApprover,
total_claimed - claimed as previousMTClaimed
from (
select t.*,
row_number() over(partition by contractor order by pcID desc) rn,
sum(approver) over(partition by contractor) total_approver,
sum(claimed) over(partition by contractor) total_claimed
from mytable t
) t
where rn = 1

How to get SUM of a column of a child table when query parent table?

I have two tables
Account Table (account)
id name
1 Account 1
2 Account 2
3 Account 3
4 Account 2
Transaction Table (transaction)
id account_id type amount datetime
1 1 credit 500 2020-04-01 06:00:00
2 1 credit 300 2020-04-01 06:00:00
3 2 credit 100 2020-04-01 06:00:00
4 2 debit 50 2020-04-01 06:00:00
5 1 debit 600 2020-04-01 06:00:00
6 3 credit 1000 2020-04-01 06:00:00
7 1 credit 100
My target is to get account id, name, balance in one query. The balance will be calculated from the transaction table as SUM of Credit Amount - SUM of Debit Amount for a given account.
Target Output
id name balance
1 Account 1 300
2 Account 2 50
3 Account 3 1000
4 Account 4 0
Possible Query
SELECT id, name, ((SELECT SUM(amount) FROM transaction WHERE type = 'credit' AND account_id = {ACCOUNT_ID} ) - (SELECT SUM(amount) FROM transaction WHERE type = 'debit' AND account_id = {ACCOUNT_ID} )) as balance
Is it possible to do this in one query and If yes How to do it.
You could do the aggregation in a derived table to avoid the issues of having to group by a lot of fields in the top level. For example:
SELECT a.id, a.name, COALESCE(b.balance, 0) AS balance
FROM account a
LEFT JOIN (
SELECT account_id,
SUM(CASE WHEN type='credit' THEN amount ELSE 0 END) -
SUM(CASE WHEN type='debit' THEN amount ELSE 0 END) AS balance
FROM transaction
GROUP BY account_id
) b ON b.account_id = a.id
Output:
id name balance
1 Account 1 300
2 Account 2 50
3 Account 3 1000
4 Account 2 0
Demo on SQLFiddle
You need a left join of account to transaction so you can group by each account and conditionally sum the amount depending on the type:
select
a.id, a.name,
sum(case when type = 'credit' then 1 else -1 end * coalesce(t.amount, 0)) balance
from account a left join transaction t
on t.account_id = a.id
group by a.id, a.name

SQL Query for calculating current balance from opening

I need you help me to make a sql query for balance calculation.
Please check out my table below. "opening" only used for the first time for calculation.
Thanks very much
Here is my table
trx_no Opening debit credit
ab123 200 0 100
ab456 200 0 50
ab789 200 0 50
cd123 200 50 0
cd456 200 0 10
cd789 200 0 40
Here is the expected result
trx_no Opening debit credit balance
ab123 200 0 100 300
ab456 200 0 50 350
ab789 200 0 50 400
cd123 200 50 0 350
cd456 200 0 10 360
cd789 200 0 40 400
Assuming that your trx_no column would always be a fixed width of 5 characters, and that lexicographical sorting indicates the age of the transaction, then we can try using a correlated subquery here to find the rolling balance:
SELECT
trx_no,
Opening,
debit,
credit,
Opening + (SELECT SUM(t2.credit - t2.debit)
FROM yourTable t2
WHERE t2.trx_no <= t1.trx_no) AS balance
FROM yourTable t1
ORDER BY
trx_no;
If you are using MySQL 8+, then window functions make this a bit less verbose:
SELECT
trx_no,
Opening,
debit,
credit,
Opening +
SUM(credit - debit) OVER (OVER ORDER BY trx_no) AS balance
FROM yourTable t1
ORDER BY
trx_no;
One more question, how about the query to get the last balance with a single query?
Thank you
trx_no Opening debit credit balance lastbalance
ab123 200 0 100 300 400
ab456 200 0 50 350 400
ab789 200 0 50 400 400
cd123 200 50 0 350 400
cd456 200 0 10 360 400
cd789 200 0 40 400 400
Edit:
I use this query, I got the correct result but I am not sure that this query is right (good) or not :
SELECT trx_no, Opening, debit, credit, Opening + (SELECT SUM(t2.credit - t2.debit) FROM yourTable t2 WHERE t2.trx_no <= t1.trx_no) AS balance, Opening + (SELECT SUM(credit - debit) FROM yourTable ) AS lastbalance FROM yourTable t1 ORDER BY trx_no;

The sum of Opening and Closing Balances for each Financial Year Grouped by Category in MYSQL

I have used three days attempting how to figure out this with no avail.I also did some search even from this forum but failed too.
It might look like Duplicate Question but to be honest this is different from others has been asked.
My question is how to get the sum of Balance Carry forward C/F and Closing balance for each financial year being GROUPED BY loan_id For each Date Range ie.Financial year?
transaction_t
Na loan_id date credit_amount debit_amount
1 1 2017-01-01 5,000 4,000
2 1 2017-05-01 6,000 2,000
3 2 2017-10-01 1,000 1,500
4 1 2018-10-30 2,000 400
5 2 2018-11-01 12,00 1,000
6 2 2019-01-15 1,800 500
7 1 2019-05-21 100 200
The above table schema and its data have mysql fiddle here I have also read this thread MySQL Open Balance Credit Debit Balance which is only working for single user.
So far I have tried:
SELECT loan_id,
SUM(credit)-(SELECT SUM(a.debit) FROM transaction_t a
WHERE a.transaction_date
BETWEEN '2019-01-01' AND '2020-12-31' AND a.loan_id = loan_id) AS OpeningBalance,
sum(credit),sum(debit),
(#OpeningBalance+SUM(credit))-SUM(debit) AS closing_balance
FROM transaction_t tr
WHERE transaction_date BETWEEN DATE('2019-01-01') AND DATE('2020-12-31')
GROUP BY loan_id
But the above is not giving correct results ,How do i get the results like these ones?
A: Query made for date between 2017-01-01 and 2018-12-31
loan_id opening_balance sum(credit_amount) sum(debit_amount) closing_balance
1 0 13,000.00 6,400.00 6,600.00
2 0 2,200.00 2,500.00 -300
B: Query made for date between 2019-01-01 and 2020-12-31
loan_id opening_balance sum(credit_amount) sum(debit_amount) closing_balance
1 6,600 100.00 200.00 6,500.00
2 -300 1,800.00 500.00 1,000
You are looking for conditional aggregation.
The key thing is that you need to start scanning the table from the beginning of the history in order to generate the initial balance. Then you just need to adjust the conditional sums:
Consider:
SET #start_date = '2017-01-01';
SET #end_date = '2018-12-31';
SELECT
loan_id,
SUM(CASE WHEN transaction_date < #start_date THEN credit - debit ELSE 0 END) opening_balance,
SUM(CASE WHEN transaction_date BETWEEN #start_date AND #end_date THEN credit ELSE 0 END) sum_credit,
SUM(CASE WHEN transaction_date BETWEEN #start_date AND #end_date THEN debit ELSE 0 END) sum_debit,
SUM(CASE WHEN transaction_date <= #end_date THEN credit - debit ELSE 0 END) closing_balance
FROM transaction_t
WHERE transaction_date <= #end_date
GROUP BY loan_id
In your DB Fiddle, this returns:
loan_id opening_balance sum_credit sum_debit closing_balance
1 0 13000 6400 6600
2 0 2200 2500 -300
And when changing the dates to 2020-2021:
loan_id opening_balance sum_credit sum_debit closing_balance
1 6600 100 200 6500
2 -300 1800 500 1000
NB: that was a well-asked question, that SO could use more of!

MySQL SUM If More Than & Less Than

I need to sum from price column if they are have more than value 200.
Otherwise, I need to sum if they are have less than value 200.
The result I want to grouping them by Date.
Please see the orders table:
price date_transaction
537 2014-03-28 11:44:40
123 2014-03-28 14:35:21
96 2014-03-28 15:43:17
341 2014-03-28 16:12:42
309 2014-03-29 15:47:48
223 2014-03-29 19:22:28
115 2014-03-29 23:31:34
109 2014-03-29 23:44:16
I want to result like this below:
date_transaction sum_of_two_hundred_or_more sum_of_less_than_200
2014-03-28 878 219
2014-03-29 532 224
2014-03-30 0 0
2014-03-31 0 0
And here is what I've tried, but it only show the more than 200 only.
SELECT
DISTINCT DATE(date_transaction) AS date_transaction,
SUM(price) AS sum_of_two_hundred_or_more
FROM orders
WHERE price > 200
AND date_transaction BETWEEN '2014-03-26' AND '2014-03-31' + INTERVAL 1 DAY
GROUP BY DATE(date_transaction)
ORDER BY DATE(date_transaction) ASC;
This should work:
SELECT DATE(date_transaction) AS date_transaction,
SUM (CASE WHEN price >= 200 THEN price ELSE 0 END) AS sum_of_two_hundred_or_more
SUM (CASE WHEN price < 200 THEN price ELSE 0 END) AS sum_of_two_hundred_less
FROM orders
WHERE date_transaction BETWEEN '2014-03-26' AND '2014-03-31' + INTERVAL 1 DAY
GROUP BY DATE(date_transaction)
ORDER BY DATE(date_transaction) ASC