MYSql Sub Query with multiple select statement - mysql

I've got a table that looks like this
Savings
ID | Name | Type | Amount | Date
1 | Alex | Cash | 100 | 2019-06-10
2 | Nick | CHQ | 500 | 2019-06-10
3 | Mike | Cash | 700 | 2019-06-10
4 | Luke | CHQ | 200 | 2019-06-10
5 | Alex | Card | 300 | 2019-06-10
6 | Alex | Card | 100 | 2019-06-10
7 | Luke | Cash | 900 | 2019-06-10
8 | Alex | Cash | 400 | 2019-06-10
9 | Mike | CHQ | 200 | 2019-06-10
is it possible to sort it out in this manner using only 1 select statement?
Final Output
Name | Total Amount | Total Cash | Total Chq
Mike | 900 | 700 | 200
Luke | 1100 | 900 | 200
Alex | 500 | 500 | 0
Nick | 500 | 0 | 500
This is my current statement
SELECT
name,
SUM(amount)
FROM
savings
WHERE
date = '2019-06-10' AND
type = 'Cash' OR date = '2019-06-10' AND
type = 'Chq'
GROUP BY
name
ORDER BY
SUM(amount) DESC
All help appreciated. thanks

use conditional aggregation with case when expression
SELECT name, sum(amount) as 'Total Amount',
SUM(case when type = 'Cash' then amount end) as 'Total Cash',
SUM(case when type = 'Chq' then amount end) as 'Total Chq'
FROM savings
where date = '2019-06-10' group BY name

For example:
select
Name,
sum(Amount) 'Total Amount',
sum(if(Type = 'Cash', Amount, 0)) 'Total Cash',
sum(if(Type = 'CHQ', Amount, 0)) 'Total Chq'
from savings
group by Name;

Related

Is it possible to get the total count of transaction and group by 15 mins interval

I need to get a result that will display the total count of transaction type and group it with 15 mins interval based on the "Intime" column.
Sample Dataset -- Database Name is transactions
--------------------------------------
| InTime | TransactionType | PaidAmt |
--------------------------------------
| 09:03 | PickUp | 10.02 |
| 09:09 | Delivery | 5.05 |
| 09:14 | Delivery | 3.99 |
| 09:15 | Delivery | 1.99 |
| 09:20 | PickUp | 10.35 |
| 09:23 | PickUp | 23.01 |
| 09:33 | PickUp | 10.06 |
| 09:44 | Delivery | 1.99 |
---------------------------------------
This is the desired result.
-------------------------------------------------------------------------------
| TimeFrame | NumberofPickUp | TotalPickUp | NumberofDelivery | TotalDelivery |
-------------------------------------------------------------------------------
| 09:00 | 1 | 10.02 | 2 | 9.04 |
| 09:15 | 2 | 33.36 | 1 | 1.99 |
| 09:30 | 1 | 10.06 | 1 | 1.99 |
-------------------------------------------------------------------------------
You can do this by rounding down all your time values to the next lower 15 minute boundary, which you can do with
SEC_TO_TIME(FLOOR(TIME_TO_SEC(InTime) / 900) * 900)
You can then use this value to GROUP BY, and conditional aggregation to get the totals you need:
SELECT SEC_TO_TIME(FLOOR(TIME_TO_SEC(InTime) / 900) * 900) AS TimeFrame,
SUM(TransactionType = 'Pickup') AS `Number of Pickup`,
ROUND(SUM(CASE WHEN TransactionType = 'Pickup' THEN PaidAmt ELSE 0 END), 2) AS `Total Pickup`,
SUM(TransactionType = 'Delivery') AS `Number of Delivery`,
ROUND(SUM(CASE WHEN TransactionType = 'Delivery' THEN PaidAmt ELSE 0 END), 2) AS `Total Delivery`
FROM transactions
GROUP BY TimeFrame
Output:
TimeFrame Number of Pickup Total Pickup Number of Delivery Total Delivery
09:00:00 1 10.02 2 9.04
09:15:00 2 33.36 1 1.99
09:30:00 1 10.06 1 1.99
Demo on dbfiddle
If there are timeframes of interest that are not present in your table, you can most easily generate the appropriate 0 values in your application code.

How to SUM values for DISTINCT values of another column and GROUP BY date?

How can we SUM amount for each activity only on same date and output a row for each date? This query is not working.
SELECT SUM(amount), type, date FROM table GROUP BY DISTINCT date;
Table
+----+------------+-----------+---------+
| id | date | activity | amount |
+----+------------+-----------+---------+
| 1 | 2017-12-21 | Shopping | 200 |
| 2 | 2017-12-21 | Gift | 240 |
| 3 | 2017-12-23 | Give Away | 40 |
| 4 | 2017-12-24 | Shopping | 150 |
| 5 | 2017-12-25 | Give Away | 120 |
| 6 | 2017-12-25 | Shopping | 50 |
| 7 | 2017-12-25 | Shopping | 500 |
+----+------------+-----------+---------+
Required Result
+------------+-----------+------+-----------+
| date | Shopping | Gift | Give Away |
+------------+-----------+------+-----------+
| 2017-12-21 | 200 | 240 | |
| 2017-12-23 | | | 40 |
| 2017-12-24 | 150 | | |
| 2017-12-25 | 550 | | 120 |
+------------+-----------+------+-----------+
Use:
select `date`,
sum(if (activity='Shopping', amount, null)) as 'Shopping',
sum(if (activity='Gift', amount, null)) as 'Gift',
sum(if (activity='Give Away', amount, null)) as 'Give Away'
from table
group by `date`
You can try this. It returns exact result that you want
SELECT t.date,
SUM(t.shopping_amount) AS shopping,
SUM(t.gift_amount) AS gift,
SUM(t.give_away_amount) AS give_away
FROM
(
SELECT p.`date`, p.`activity`, p.`amount` AS shopping_amount,
0 AS gift_amount, 0 AS give_away_amount
FROM products p
WHERE p.`activity` = 'Shopping'
UNION
SELECT p.`date`, p.`activity`, 0 AS shopping_amount,
p.amount AS gift_amount, 0 AS give_away_amount
FROM products p
WHERE p.`activity` = 'Gift'
UNION
SELECT p.`date`, p.`activity`, 0 AS shopping_amount,
0 AS gift_amount, p.amount AS give_away_amount
FROM products p
WHERE p.`activity` = 'Give Away'
) t
GROUP BY t.date
Hmmm, you can't pivot your results into column headers unless you know all possible values as demonstrated by slaasko but you can get the results using sql into a form which can be pivoted using your display tool ( e.g. slice of BI tool).
SELECT SUM(amount), activity, date FROM table GROUP BY date, activity;

MySQL Select SUM of Total Transaction

I have two tables transaction and detailtransaction, the relation is 1 to N
, here's transaction Table
+-----+----------+--------+---------------------+------------+-------------+---------+---
| ID | TransactionDate | GrandTotal | DownPayment | Status | DatePaid |
+-----+----------+--------+---------------------+------------+-------------+---------+---
| 173 | 2018-01-03 22:05:00 | 26000.00 | 26000.00 | PAID OFF | 2018-01-03 21:05:52 |
| 174 | 2018-01-01 22:06:00 | 26000.00 | 0.00 | PAID OFF | 2018-01-03 22:05:52 |
| 175 | 2018-01-02 22:06:00 | 60000.00 | 10000.00 | - 50000 | 2018-01-03 21:06:55 |
| 176 | 2018-01-03 22:08:00 | 90000.00 | 50000.00 | - 40000 | 2018-01-03 21:08:19 |
| 178 | 2018-01-03 22:34:00 | 70000.00 | 70000.00 | PAID OFF | 2018-01-03 21:35:00 |
| 179 | 2018-01-03 23:13:00 | 52000.00 | 52000.00 | PAID OFF | 2018-01-03 22:13:35 |
+-----+----------+--------+---------------------+------------+-------------+---------+---
and here's transactiondetail table
+----+---------------+-----------+---------------+--------+------+----------+----------+------------+
| ID | TransactionID | ProductID | ServiceID | UserID | Tax | Discount | Quantity | PriceTotal |
+----+---------------+-----------+---------------+--------+------+----------+----------+------------+
| 1 | 173 | NULL | SV031 | 7 | NULL | 0 | 0 | 26000.00 |
| 2 | 174 | NULL | SV032 | 7 | NULL | 0 | 0 | 26000.00 |
| 3 | 175 | NULL | SV033 | 7 | NULL | 0 | 0 | 60000.00 |
| 4 | 176 | 8 | NULL | 7 | NULL | 0 | 1 | 30000.00 |
| 5 | 176 | NULL | SV034 | 7 | NULL | 0 | 0 | 60000.00 |
| 7 | 178 | 5 | NULL | 7 | NULL | 0 | 1 | 70000.00 |
| 8 | 179 | NULL | SV036 | 7 | NULL | 0 | 0 | 26000.00 |
| 9 | 179 | NULL | SV037 | 7 | NULL | 0 | 0 | 26000.00 |
+----+---------------+-----------+---------------+--------+------+----------+----------+------------+
There's 2 things can be made in 1 transaction, buy a product or a service. So there's a ServiceID and ProductID on TransactionDetail's.
on ID 173, someone made transaction of service and paid off
ID 174, someone made transaction 3 days ago and paid it today
ID 175 transaction of service and put Downpayment of 10k
ID 176 transaction of 1 product cost 30k and service cost 60k, downpayment is 50k (minus 40k)
ID 178 buying some product cost 70k
ID 179 2 service and paid off the transaction
The rule is product cant be paid later, only service can
I want to make a report of daily income, how to sum is based on the DatePaid, the result i should get is 26k + 26k + 10k + 50k + 70K + 52k = 230K
i've tried to get SUM by joining the table what i got is it repeating the value, like if theres 2 service in 1 transaction, it sums like its 2 transaction. here's what i tried, i got 336k
SELECT SUM(CASE WHEN Status = 'PAID OFF' THEN GrandTotal ELSE DownPayment END) FROM `transaction` a LEFT JOIN transactiondetail b ON a.ID = b.TransactionID WHERE DatePaid BETWEEN '2018-01-03 00:00:00' AND '2018-01-03 23:59:59'
Also how can I sum only transactiondetail with serviceID so that the product isnt the sum.
Thanks
First of all, if all you need is just the sum of amount, you don't need to join with transaction details. Just the below query will do.
SELECT SUM(CASE WHEN Status = 'PAID OFF' THEN GrandTotal ELSE DownPayment END) FROM transaction WHERE DatePaid BETWEEN '2018-01-03 00:00:00' AND '2018-01-03 23:59:59'
Secondly, what details do you need from transaction details table, do you want to see the amount by product and service grouped.
To get the total sum :
SELECT SUM(CASE WHEN Status = 'PAID OFF' THEN GrandTotal ELSE DownPayment END) FROM `transaction` WHERE DatePaid BETWEEN '2018-01-03 00:00:00' AND '2018-01-03 23:59:59'
removing products can by done by checking if product_id is not null and to get total use sub query as:
SELECT SUM(total) FROM( SELECT SUM(CASE WHEN Status = 'PAID OFF' THEN GrandTotal ELSE DownPayment END) AS total FROM `transaction` a LEFT JOIN transactiondetail b ON a.ID = b.TransactionID WHERE DatePaid BETWEEN '2018-01-03 00:00:00' AND '2018-01-03 23:59:59' AND ProductID IS NULL GROUP BY a.ID ) as t

Mysql Join Union Query By Date

I have 2 tables
and i want the result as this using join query. I don't have any Idea
Join Query By
TBL_SUCCESS_ORDER
------------------------
id | date | amount
-------------------------
1 | 2017-01-01 | 1000
2 | 2017-01-06 | 300
3 | 2017-01-29 | 50
4 | 2017-02-02 | 100
5 | 2017-02-16 | 400
6 | 2017-03-01 | 500
7 | 2017-04-03 | 1200
TBL_FAIL_ORDER
------------------------
id | date | amount
-------------------------
1 | 2017-01-03 | 400
2 | 2017-01-07 | 300
3 | 2017-01-24 | 50
4 | 2017-02-02 | 100
5 | 2017-04-07 | 200
RESULT
------------------------------------------------------------------
year | month | sum_of_succes_amount | sum_of_fail_amount | total
------------------------------------------------------------------
2017 | January | 1350 | 750 | 2100
2017 | Febuary | 500 | 100 | 600
2017 | March | 500 | 0 | 500
2017 | April | 1200 | 200 | 1400
I been stack for a whole week i did not get it . When i connect api json
TBL_PENDING_ORDER
------------------------
id | date | amount
-------------------------
1 | 2017-04-03 | 600
2 | 2017-05-07 | 600
RESULT
-----------------------------------------------------------------------------------------
year | month | sum_of_succes_amount | sum_of_fail_amount | sum_of_pending_amount |total
-----------------------------------------------------------------------------------------
2017 | January | 1350 | 750 | 0 | 2100
2017 | Febuary | 500 | 100 | 0 | 600
2017 | March | 500 | 0 | 0 | 500
2017 | April | 1200 | 200 | 600 | 2000
2017 | May | 0 | 0 | 600 | 600
What if I add The third table ? TBL_PENDING_ORDER
You can use the following solution using UNION ALL AND GROUP BY:
SELECT
YEAR(x.date),
MONTH(x.date),
SUM(CASE WHEN x.type = 'S' THEN amount ELSE 0 END) AS sum_of_succes_amount,
SUM(CASE WHEN x.type = 'F' THEN amount ELSE 0 END) AS sum_of_fail_amount,
SUM(amount) AS total
FROM (
SELECT id, date, amount, 'S' AS type FROM TBL_SUCCESS_ORDER
UNION ALL
SELECT id, date, amount, 'F' AS type FROM TBL_FAIL_ORDER
)x GROUP BY YEAR(x.date), MONTH(x.date)
You want to add the third table TBL_PENDING_ORDER?
SELECT
YEAR(x.date),
MONTH(x.date),
SUM(CASE WHEN x.type = 'S' THEN amount ELSE 0 END) AS sum_of_succes_amount,
SUM(CASE WHEN x.type = 'F' THEN amount ELSE 0 END) AS sum_of_fail_amount,
SUM(CASE WHEN x.type = 'P' THEN amount ELSE 0 END) AS sum_of_pending_amount,
SUM(amount) AS total
FROM (
SELECT id, date, amount, 'S' AS type FROM TBL_SUCCESS_ORDER
UNION ALL
SELECT id, date, amount, 'F' AS type FROM TBL_FAIL_ORDER
UNION ALL
SELECT id, date, amount, 'P' AS type FROM TBL_PENDING_ORDER
)x GROUP BY YEAR(x.date), MONTH(x.date)

mysql, subquery or join in group by condition

+------------+------+-----------------------+
| invoice | amount | paid | date |
+------------+--------+--------+------------+
| 1001 | 55 | 1 | 2015-01-01 |
+------------+--------+--------+------------+
| 1002 | 30 | 0 | 2015-02-01 |
+------------+--------+--------+------------+
| 1003 | 59 | 1 | 2015-02-01 |
+------------+--------+--------+------------+
| 1004 | 78 | 0 | 2015-03-01 |
+------------+--------+--------+------------+
| 1005 | 65 | 1 | 2015-03-01 |
+------------+--------+--------+------------+
| 1006 | 107 | 0 | 2015-04-01 |
+------------+--------+--------+------------+
For example, if I have above table,
I want to get total invoice amount and paid amount.
for total invoice amount
select sum(amount) from invoice group by DATE_FORMAT(date,"%Y-%m")
to get paid invoice
select sum(amount) from invoice where paid=1 group by DATE_FORMAT(date,"%Y-%m")
How can I combine it into single query?
Use IF to sum just the rows with paid = 1.
SELECT DATE_FORMAT(date, '%Y-%m') AS month,
SUM(amount) AS total,
SUM(IF(paid = 1, amount, 0)) AS total_paid
FROM invoice
GROUP BY month;