Get Available Balance from transaction table using MYSQL - mysql

I tried to get available balance of specific account number using SQL QUERY my code is below:
select sum(amount) as Cr form transaction where credit=1 and account_no=2549
Union
select sum(amount) as Dr form transaction where debit=1 and account_no=2549
Difference Cr-Dr

You could use case to get sum for credit and debit in single query as
select sum(case when credit=1 then amount else 0 end) as Cr ,
sum(case when debit=1 then amount else 0 end) Dr,
sum(case when credit=1 then amount else 0 end) - sum(case when debit=1 then amount else 0 end) as available_balance
from `transaction`
where account_no=2549

The below query should work for you to get available balance.
select sum(amt) from
(select
case
when credit=1 then amount
when debit=1 then -amount
else 0
end as amt
from transaction
where account_no = 5294)t;

Related

How to sum a field for two times with two conditions in the same SQL Statements

I have the following SQL statements:
Select SUM (amount) from accounts Where acctype = 0;
and
Select SUM (amount) from accounts Where acctype = 1;
I want to find the difference between the amount in two cases in a single SQL statement.
This is called conditional aggregation:
SELECT
SUM(case when acctype = 0 then amount else 0 end) AS sum0,
SUM(case when acctype = 1 then amount else 0 end) AS sum1,
SUM(case when acctype = 0 then amount else 0 end) -
SUM(case when acctype = 1 then amount else 0 end) AS diff
FROM accounts;
You can use conditional aggregation:
select sum(case when acctype = 0 then amount
when acctype = 1 then - amount
end) as diff
from accounts;

Mysql Query: How many sum() recommended in single query?

I have 70 different types of accounts. And I am fetching the data as per the account type.
The query like this,
$mainData = "SELECT
count(*) AS totalRows,
sum(pay) as totalPay
sum(case when account_type = 1 then 1 else 0 end) AS account_1_Total,
sum(case when account_type = 1 then pay else 0 end) AS account_1_Pay,
sum(case when account_type = 2 then 1 else 0 end) AS account_2_Total,
sum(case when account_type = 2 then pay else 0 end) AS account_2_Pay,
{all_account_types_here}
FROM account_table";
In the end, those sum() are about more than 140.
So the question is, how many sum() is recommended in a single query?
Thanks!
EDITED:
The GROUP BY is the solution of it.

Query to get column totals and Subtract MySQL

I have a transaction table
I am able to get all the debit and credit transaction totals by property separately using these queries
SELECT property, SUM(amount) as creditamount FROM transactions WHERE transactions.type="CREDIT" GROUP BY property
SELECT property, SUM(amount) as debitamount FROM transactions WHERE transactions.type="DEBIT" GROUP BY property
I am facing difficulty in having these two queries done in a single query so that i can subtract credit amount and debit amount for each row and list it according to the property
I tried using sub queries but multiple rows are returned.
Any way to achieve this?
Conditional aggregation may be what you are looking for.
SELECT property,
SUM(case when type = 'Credit' then amount else 0 end) as creditamount,
SUM(case when type = 'Debit' then amount else 0 end) as debitamount,
SUM(case when type = 'Credit' then amount else 0 end) -
SUM(case when type = 'Debit' then amount else 0 end) as diff
FROM transactions
GROUP BY property
Use self join
select debitamount , creditamount
from (select sum(amount) income
from transactions
WHERE transactions.type="CREDIT"
GROUP BY property) i
JOIN (select sum(rate) debitamount
from transactions
WHERE transactions.type="DEBIT"
GROUP BY property) e
Refer this question

MySQL Attendance System

I have made an attendance system for a project, However I'm stuck right now by trying to create a query.
SELECT
COUNT(Students.idStudents) total,
SUM(case when Attendance.status LIKE 'present' then 1 else 0 end) present,
SUM(case when Attendance.status LIKE 'late' then 1 else 0 end) late,
SUM(case when Attendance.status is null then 1 else 0 end) absents
FROM Students, Schools, Tags LEFT JOIN Attendance
ON Attendance.tagCode = Tags.tagCode
WHERE Schools.idSchools = Students.idSchools
AND Tags.idStudents = Students.idStudents
This code works and generates an attendance. However this will show all the dates.
When I add in another line to specify date
AND Attendance.date = DATE(NOW());
It will not show anything..
There's 'Present', 'Late' status for the attendance however if the student's record in that table doesn't exist, it is considered as absent.
How do I do that?
Assuming you're using a case insensitive collation, your purported solution can be rewritten as follows:
SELECT COUNT(p.idStudents) total
, SUM(CASE WHEN a.status = 'present' THEN 1 ELSE 0 END) present -- [or just SUM(a.status = 'present')]
, SUM(CASE WHEN a.status = 'late' THEN 1 ELSE 0 END) late
, SUM(CASE WHEN a.status IS NULL THEN 1 ELSE 0 END) absents
FROM Students p
JOIN Schools s
ON s.idSchools = p.idSchools
JOIN Tags t
ON t.idStudents = p.idStudents
LEFT
JOIN Attendance a
ON a.tagCode = t.tagCode
AND a.date = CURDATE() ;
For next time: Your ERD shows 10 tables, but only 4 feature in this problem. If a table isn't likely to be part of the proposed solution, don't show it. Don't provide pictures. Instead, where possible, provide proper DDLs (and/or an sqlfiddle), TOGETHER WITH THE DESIRED RESULT SET based upon a minimal, but properly representative data set.
Welcome to SO.
Figured it out..
SELECT
COUNT(Students.idStudents) total,
SUM(case when Attendance.status LIKE 'present' then 1 else 0 end) present,
SUM(case when Attendance.status LIKE 'late' then 1 else 0 end) late,
SUM(case when Attendance.status is null then 1 else 0 end) absents
FROM Students, Schools, Tags LEFT JOIN (SELECT * FROM Attendance WHERE Attendance.date = NOW()) AS Attendance
ON Attendance.tagCode = Tags.tagCode
WHERE Schools.idSchools = Students.idSchools
AND Tags.idStudents = Students.idStudents
The problem with the original query was you were asking for students where their attendance was "NOW" where no student was ever now but they are current attending classes. They may have signed in at 8am and you run the query at 10am. You'd need to manipulate the datetime to choose your start time and end time based on the current day.
timestampadd(HOUR, 08, CURDATE()) - this will give you 8am, you'd then query for when attendance.date is greater than or equal to 8am and then potentially less than or equal to 4pm?

SQL SUM values where 2 conditions are met

I am trying to create a query that calculates credit and debit for each month. the values are in column called SUM and there is a field for each record that says if it is credit or debit (ex_type). I have manage to get the total sum of all the fields (although it is not correct as well - cos I am just sum all the values no matter if it is credit or debit).
SELECT sum(sum) as total FROM acc where month='$month'
But I cannot figure out for to do it for Credit and Debit
So, to summarize... I want to have the following two queries combined in one.
1. Select sum(sum) as Debit from acc where ex_type='Debit' and month='$month'
2. Select sum(sum) as Credit from acc where ex_type='Credit' and month='$month'
So, any help is much appreciated.
Try this with CASE
Select sum(CASE WHEN ex_type='Debit' THEN `sum` ELSE 0 END) as Debit,
sum(CASE WHEN ex_type='Credit' THEN `sum` ELSE 0 END) as Credit FROM ...
This should work:
Select sum(d.sum) as Debit,
(Select sum(c.sum) from acc c where c.ex_type='Credit' and c.month='$month') as Credit
from acc d where d.ex_type='Debit' and d.month='$month'
However if you supply more details on other fields one could inner join onto the same table and that may be slightly more efficient.
Maybe duplicate post :
SUM() based on a different condition to the SELECT
Try this :
Select
SUM(CASE WHEN ex_type='Debit' THEN `sum` ELSE 0 END) as Debit,
SUM(CASE WHEN ex_type='Credit' THEN `sum` ELSE 0 END) as Credit
FROM acc
Where etc...
I'm not a MySQL expert, but you might want to try the following
SELECT
SUM(CASE WHEN ex_type='Debit' THEN sum ELSE 0 END CASE) as SumOfDebit,
SUM(CASE WHEN ex_type='Credit' THEN sum ELSE 0 END CASE) as SumOfCredit,
FROM acc
WHERE
month = '$month'
MySQL reference 5.0 deals with the CASE statement if this doesn't work as expected
SELECT
SUM(sum) as total
SUM(IF(ex_type='Debit',sum,0)) Debit
SUM(IF(ex_type='Credit',sum,0)) Credit
FROM acc
WHERE month='$month'
The simplest (and coolest) way is:
SELECT
SUM((ex_type='Debit') * sum) Debit,
SUM((ex_type='Credit') * sum) Credit,
FROM acc
WHERE month = '$month'
This works because in mysql, true is 1 and false is 0.