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)
Related
I have two table first user table is user's data second table is user_volume table is user cost volume I want one sql query and get user and get monthly sum of cost what is SQL Query?
user_volume
id | user_id | volume | created_at
1 | 1 | 66.00 | 2018-03-03 15:36:45
2 | 1 | 77.00 | 2018-03-03 15:36:21
3 | 1 | 88.00 | 2018-03-03 15:36:11
4 | 2 | 99.00 | 2018-03-03 19:36:15
5 | 2 | 65.05 | 2018-04-04 21:30:07
6 | 2 | 99.00 | 2018-04-04 19:36:15
7 | 2 | 65.05 | 2018-04-04 21:30:07
8 | 1 | 22.00 | 2018-04-04 15:36:45
9 | 1 | 44.00 | 2018-04-04 15:36:21
10 | 1 | 33.00 | 2018-04-04 15:36:11
11 | 2 | 13.00 | 2018-04-04 15:36:45
12 | 2 | 224.00 | 2018-04-04 15:36:21
13 | 2 | 651.00 | 2018-04-04 15:36:11
user
id | name | surname
1 | X | Y
result
user_id | date1(03-2018) | date2(04-2018)
1 | (231) | (99)
2 | (99) | (888)
You seem to want conditional aggregation:
select uv.id as user_id,
sum(case when created_at >= '2018-03-01' and created_at < '2018-04-1'
then volume else 0
end) as volume_month1,
sum(case when created_at >= '2018-04-01' and created_at < '2018-05-1'
then volume else 0
end) as volume_month2
from user_volume uv
group by uv.id
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.
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;
I have a query where I suppose to show debit, credit & balance. I have no balance column in table. I calculate the balance from debit & credit.
I tried to create a balance column where balance will be stored and display it from table. But if I update or delete any row the balance will not proper.
I found the below code on SO. But it works when there is one row for a one date, multiple columns for a date is not working properly. It display the balance with day by day, I want to display balance with row by row.
MySQL
SELECT
m.`id`,
m.`date`,
m.`credit`,
m.`debit`,
SUM(t.`credit`) - SUM(t.`debit`) AS `balance`
FROM `cash_book` m
JOIN (
SELECT
`id`,
`date`,
`credit`,
`debit`
FROM
`cash_book`
) t ON t.`date` <= m.`date`
WHERE `customer_id` = 1
GROUP BY
m.`id`
ORDER BY m.`date` ASC
It's return the result like this:
Date Debit Credit Balance
2016-11-27 0 2000 2000
2016-12-02 0 500000 585000 //same result for date 2016-12-02
2016-12-02 15000 0 585000 //same result for date 2016-12-02
2016-12-02 0 100000 585000 //same result for date 2016-12-02
2016-12-03 1200 0 583800
2016-12-04 3160 0 580540 //same result for date 2016-12-04
2016-12-04 100 0 580540 //same result for date 2016-12-04
2016-12-05 30 0 580510
2016-12-06 0 150 580660
But I want the result like this:
Date Debit Credit Balance
2016-11-27 0 2000 2000
2016-12-02 0 500000 502000
2016-12-02 15000 0 487000
2016-12-02 0 100000 587000
2016-12-03 1200 0 585800
2016-12-04 3160 0 582640
2016-12-04 100 0 582540
2016-12-05 30 0 582510
2016-12-06 0 150 582660
is it this what you looking for ?
SELECT
`id`,
`date`,
`credit`,
`debit`,
#balance := #balance + credit-debit AS balance
FROM `cash_book`
CROSS JOIN ( SELECT #balance := 0) as init
ORDER BY `date` ASC ;
sample
mysql> SELECT * FROM cash_book;
+------+------------+-------+--------+
| id | date | debit | credit |
+------+------------+-------+--------+
| 1 | 2016-11-27 | 0 | 2000 |
| 2 | 2016-12-04 | 3160 | 0 |
| 3 | 2016-12-02 | 15000 | 0 |
| 4 | 2016-12-03 | 1200 | 0 |
| 5 | 2016-12-05 | 30 | 0 |
| 6 | 2016-11-29 | 0 | 10000 |
| 7 | 2016-01-05 | 0 | 0 |
| 8 | 2016-12-01 | 2000 | 0 |
| 9 | 2016-11-29 | 10000 | 0 |
| 10 | 2016-12-02 | 2000 | 100000 |
| 11 | 2016-12-06 | 2000 | 150 |
| 12 | 2016-12-02 | 2000 | 500000 |
+------+------------+-------+--------+
12 rows in set (0,00 sec)
mysql> SELECT
-> `id`,
-> `date`,
-> `credit`,
-> `debit`,
-> #balance := #balance + credit-debit AS balance
-> FROM `cash_book`
-> CROSS JOIN ( SELECT #balance := 0) as init
-> ORDER BY `date` ASC ;
+------+------------+--------+-------+---------+
| id | date | credit | debit | balance |
+------+------------+--------+-------+---------+
| 7 | 2016-01-05 | 0 | 0 | 0 |
| 1 | 2016-11-27 | 2000 | 0 | 2000 |
| 6 | 2016-11-29 | 10000 | 0 | 12000 |
| 9 | 2016-11-29 | 0 | 10000 | 2000 |
| 8 | 2016-12-01 | 0 | 2000 | 0 |
| 3 | 2016-12-02 | 0 | 15000 | -15000 |
| 10 | 2016-12-02 | 100000 | 2000 | 83000 |
| 12 | 2016-12-02 | 500000 | 2000 | 581000 |
| 4 | 2016-12-03 | 0 | 1200 | 579800 |
| 2 | 2016-12-04 | 0 | 3160 | 576640 |
| 5 | 2016-12-05 | 0 | 30 | 576610 |
| 11 | 2016-12-06 | 150 | 2000 | 574760 |
+------+------------+--------+-------+---------+
12 rows in set (0,00 sec)
mysql>
You can use a subquery in select part like this
SELECT
m.`id`,
m.`date`,
m.`credit`,
m.`debit`,
(select sum(n.`credit`) - sum(n.`debit`)
from `cash_book` n
where n.`id` = m.`id`
and n.`date` <= m.`date`) balance
FROM `cash_book` m
WHERE `customer_id` = 1
ORDER BY m.`date` ASC
try this query
select s.Date,s.Debit,s.credit,ABS(#b := #b + s.debit - s.credit) as balance from (select #b:= 0.0) as dummy cross join cash_book as s order by ID;
In your Query you have used JOIN, That will used to join two or more tables. Here you are using only one table. So no need of JOIN inside the query.
You can use this following simple query,
SELECT
m.id,
m.date,
m.credit,
m.debit,
(SELECT SUM(credit)-SUM(debit)
FROM`cash_book` A
WHERE A.date<=m.Date)
FROM `cash_book` m WHERE m.customer_id = 1 ORDER BY m.Date ASC
Greets! I have 12 tables, one for each month of the year:
January
+----+-----+
| id | sale|
+----+-----+
| 1 | 250 |
| 3 | 500 |
| 5 | 200 |
| 7 | 100 |
+----+-----+
February
+----+-----+
| id | sale|
+----+-----+
| 1 | 350 |
| 2 | 400 |
| 3 | 500 |
| 4 | 800 |
+----+-----+
etc.
I need to do a query where the result is something like this:
Annual Sales
+----+-----------+-----------+
| id | Sales_Jan | Sales_Feb |
+----+-----------+-----------+
| 1 | 250 | 350 |
| 2 | 0 | 400 |
| 3 | 500 | 500 |
| 4 | 0 | 800 |
| 5 | 200 | 0 |
| 7 | 100 | 0 |
+----+-----------+-----------+
Where the matching ids from both tables do not duplicate and the missing ids from other months are shown by putting a 0 or any other symbol indicating that there was not any sales that month from that id.
Thank you very much!
You can approach this using union all and aggregation:
select id,
sum(case when month = 'Jan' then sale else 0 end) as Jan_Sale,
sum(case when month = 'Feb' then sale else 0 end) as Feb_Sale,
. . .
sum(case when month = 'Dec' then sale else 0 end) as Dec_Sale
from ((select 'Jan' as month, id, sale from January) union all
(select 'Feb' as month, id, sale from February) union all
. . .
(select 'Dec' as month, id, sale from February)
) t
group by id;