I have two tables orders and customers:
select * from orders;
+------+---------------------+-------------+--------+
| oid | date | customer_id | amount |
+------+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 4 | 300 |
| 100 | 2009-10-08 00:00:00 | 3 | 15000 |
| 101 | 2008-10-08 00:00:00 | 2 | 1300 |
| 105 | 2010-10-08 00:00:00 | 1 | 400 |
| 106 | 2014-12-23 00:00:00 | 3 | 300 |
+------+---------------------+-------------+--------+
select * from customers;
+------+--------+------+-----------+----------+
| id | name | age | address | salary |
+------+--------+------+-----------+----------+
| 1 | ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | khilan | 25 | delhi | 1500.00 |
| 3 | muffy | 22 | bhopal | 8500.00 |
| 4 | suresh | 48 | mumbai | 24000.00 |
| 1 | ramesh | 32 | Ahmedabad | 300.00 |
| 5 | akil | 21 | madurai | 1000.00 |
| 6 | rajesh | 22 | delhi | 5000.00 |
+------+--------+------+-----------+----------+
What I'm trying to do is to do SUM(salary) from customers and subtract it with the SUM(amount) from orders table. I have tried with this query:
SELECT id ,NAME,SUM(salary),SUM(amount),SUM(salary)-SUM(amount)
FROM customers a LEFT JOIN orders b ON a.id=b.customer_id
GROUP BY NAME;
This will return the following result, which some of them return incorrect value:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | NULL |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 17000.00 | 15300 | 1700.00 |
| 6 | rajesh | 5000.00 | NULL | NULL |
| 1 | ramesh | 2300.00 | 800 | 1500.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
My expected output is as following:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | 1000 |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 8500 | 15300 | -6800 |
| 6 | rajesh | 5000.00 | NULL | 5000 |
| 1 | ramesh | 2300.00 | 400 | 1900.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
One way is to make calculation on orders into sub-query.
To cater for NULL value, you can use IFNULL(value,0).
SELECT id,NAME,SUM(salary),amt,SUM(salary)-IFNULL(amt,0)
FROM customers a LEFT JOIN
(SELECT customer_id, SUM(amount) amt FROM orders GROUP BY customer_id) b
ON a.id=b.customer_id
GROUP BY NAME;
Related
+----------------+
| Tables_in_test |
+----------------+
| contribution |
| expectedamount |
| registration |
+----------------+
registration table
+----+--------+-------------+
| id | reg_no | fullname |
+----+--------+-------------+
| 1 | TTI001 | JOHN JAMES |
| 2 | TTI002 | DAVID CERES |
| 3 | TTI003 | JOYCE LEE |
| 4 | TTI004 | JOEL MARTIN |
+----+--------+-------------+
espectedamount
+----+--------+---------+---------+---------+
| id | reg_no | number1 | number2 | number3 |
+----+--------+---------+---------+---------+
| 1 | TTI001 | 500 | 500 | 500 |
| 2 | TTI002 | 500 | 500 | 500 |
| 3 | TTI003 | 500 | 500 | 500 |
| 4 | TTI004 | 500 | 500 | 500 |
| 5 | TTI001 | 400 | 400 | 400 |
| 6 | TTI001 | 1000 | 1000 | 1000 |
| 7 | TTI002 | 1000 | 1000 | 1000 |
| 8 | TTI003 | 1000 | 1000 | 1000 |
| 9 | TTI004 | 1000 | 1000 | 1000 |
+----+--------+---------+---------+---------+
contribution table
+----+--------+---------+---------+---------+
| id | reg_no | number1 | number2 | number3 |
+----+--------+---------+---------+---------+
| 1 | TTI001 | 200 | 400 | 600 |
| 2 | TTI002 | 100 | 50 | 250 |
| 3 | TTI001 | 100 | 400 | 400 |
| 4 | TTI002 | 300 | 400 | 600 |
| 5 | TTI003 | 300 | 100 | 50 |
| 6 | TTI004 | 50 | 60 | 40 |
| 7 | TTI004 | 500 | 300 | 400 |
+----+--------+---------+---------+---------+
I created the following Query to join tables registration,expectedamaount,and contribution where i WANT TO SUBTRACT SUM FORM amount table from sum contribution table but am getting Wrong result
select registration.reg_no
,registration.fullname
,sum(expectedamount.number1-contribution.number1) as contribution1
,sum(expectedamount.number2-contribution.number2) as contribution2
,sum(expectedamount.number3-contribution.number3) as contribution3
FROM registration
INNER JOIN expectedamount ON registration.reg_no = expectedamount.reg_no
INNER JOIN contribution ON expectedamount.reg_no = contribution.reg_no
GROUP BY reg_no;
+--------+-------------+---------------+---------------+---------------+
| reg_no | fullname | contribution1 | contribution2 | contribution3 |
+--------+-------------+---------------+---------------+---------------+
| TTI001 | JOHN JAMES | 700 | 200 | 0 |
| TTI002 | DAVID CERES | 600 | 550 | 150 |
| TTI003 | JOYCE LEE | 200 | 400 | 450 |
| TTI004 | JOEL MARTIN | 450 | 640 | 560 |
+--------+-------------+---------------+---------------+---------------+
expected result
+-------+---------------+---------------+------------------+--------------+
|reg_no | fullname | contribution1 | contribution2 | contribution3|
+-------+---------------+---------------+------------------+--------------+
|TTI001 | JOHN JAMES | 1600 | 1100 | 900 |
|TTI002 | DAVID CERES | 1000 | 950 | 550 |
|TTI003 | JOYCE LEE | 1200 | 1400 | 1450 |
|TTI004 | JOEL MARTIN | 950 | 1140 | 1060 |
+-------+---------------+---------------+------------------+--------------+
Kindly guys help.
I think you need this -
select registration.reg_no
,registration.fullname
,sum(expected.num1-contrib.num1) as contribution1
,sum(expected.num2-contrib.num2) as contribution2
,sum(expected.num3-contrib.num3) as contribution3
FROM registration
INNER JOIN (SELECT reg_no, SUM(number1) num1, SUM(number2) num2, SUM(number3) num3
FROM expectedamount
GROUP BY reg_no) expected ON registration.reg_no = expected.reg_no
INNER JOIN (SELECT reg_no, SUM(number1) num1, SUM(number2) num2, SUM(number3) num3
FROM contribution
GROUP BY reg_no) contrib ON expected.reg_no = contrib.reg_no
GROUP BY reg_no;
I am very new to MySQL and also this is my first question if there is any mistakes please forgive me.
I have 6 tables i want to take report using this 6 tables. One of my report to access all the 6 tables and print the data as per the Query we write.Now the problem is i have write my query but this will return lot of duplicate data.How to avoid that guide me please.
Demo Data:
add_employees
-------------------
| eid | name |
-------------------
| 1 | Mohanraj |
-------------------
| 2 | pradeep |
-------------------
| 3 | kumar |
-------------------
| 4 | Murali |
-------------------
add_vehicle
---------------------
| vid | vnumber |
---------------------
| 1 | TN22BQ6226 |
---------------------
| 2 | TN37CM9014 |
---------------------
| 3 | TN38BR9217 |
---------------------
| 4 | TN38BT5680 |
---------------------
third_table
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| tid | vid | emp_id | entry_date | pick_place | start_time | drop_place | stop_time | pickupkm | drops | type_of_trip | travelkm | tamt | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 5-5-2017 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000 |2017-05-05 |
| 2 | TN22BQ6226 | 3 | 6-5-2017 | Hopes | 09:44 am | ESI | 12:30 pm | 12500 | 2500 | Cash | 1250 | 3500 |2017-05-06 |
| 3 | TN22BQ6226 | 3 | 5-5-2017 | Place1 | 03:15 pm | Place2 | 06:30 pm | 1500 | 1800 | Cash | 300 | 1500 |2017-05-05 |
fourth_table
---------------------------------------------------------------------------------------------------------------------------------------------------
| fid | vid | emp_id | expcal | exp1 | exp2 | exp3 | exp4 | exp5 | exp_amt | exp_desc | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | | | 2017-05-05 |
| 2 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | | | 2017-05-06 |
five_table
| fi_id | vid | emp_id | totkm | totamt | expenses | today_balance | handover_amt | balance_amt | handover_to | plstatus | entry_date | entry_time |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 10000 | 5000 | 100 | 4900 | 4000 | 400 | ram | PROFIT | 2017-05-05 | 04:35:21 |
| 2 | TN22BQ6226 | 3 | 1250 | 3500 | 100 | 3400 | 3000 | 200 | raj | PROFIT | 2017-05-06 | 04:36:58 |
shift
---------------------------------------------------------------------------------------------------------------------------------------------------
| sid | emp_id | vid | opeing_km | opeing_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | status |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | 1 |
| 2 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | 1 |
And i try this query i get wrong out,
SELECT a.vnumber
, a.vname
, b.eid
, b.name
, b.mobile
, c.tid
, c.vid
, c.emp_id
, c.pick_place
, c.start_time
, c.drop_place
, c.stop_time
, c.pickupkm
, c.drops
, c.type_of_trip
, c.travelkm
, c.tamt
, c.dates
, d.vid
, d.emp_id
, d.expcal
, d.exp1
, d.exp2
, d.exp3
, d.exp4
, d.exp5
, d.expamt
, d.expdesc
, d.dates
, e.emp_id
, e.vid
, e.opeing_km
, e.opeing_cash
, e.closing_km
, e.closing_cash
, e.opeing_date
, e.opeing_time
, e.closing_date
, e.closing_time
, f.vid
, f.emp_id
, f.totkm
, f.totamt
, f.expenses
, f.handover_amt
, f.balance_amt
, f.handover_to
, f.plstatus
, f.entry_date
FROM add_vehicle a
JOIN third_table c
ON a.vnumber = c.vid
JOIN add_employees b
ON b.eid = c.emp_id
JOIN fourth_table d
ON a.vnumber = d.vid
JOIN shift e
ON b.eid = e.emp_id
JOIN five_table f
ON a.vnumber = f.vid
, (SELECT #rownum := 0) r
WHERE c.type_of_trip IS NOT NULL
AND c.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND d.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND f.entry_date BETWEEN '2017-05-05' AND '2017-06-05'
AND c.vid = 'TN22BQ6226'
AND f.vid = 'TN22BQ6226'
AND e.vid = 'TN22BQ6226'
GROUP
BY c.tid
ORDER
BY c.dates
, f.entry_date DESC
i get fourth_table and five_table values are repeated like this.
one and two
I want the output like this,
| vnumber | vname | eid | name | tid | vid | emp_id | pick_place | start_time | drop_place | stop_time | pickup_km | drops | type_of_trip | travel_km | tamt | dates | vid | emp_id |expcal |exp1 | exp2 | exp3 | exp4 | exp5 | expamt| expdesc | dates | emp_id | vid | opening_km | opening_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | vid | emp_id | totkm | totamt | expenses | handover_amt | balance_amt| handover_to | plstatus | entry_date |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 1 | TN22BQ6226 | 3 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 2 | TN22BQ6226 | 3 | Hopes | 09:44 am | ESI | 12:01 pm | 1250 | 2500 | Cash | 1250 | 3500| 2017-05-06 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | 50 | |2017-05-06 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | TN22BQ6226 | 3 | 1250| 3500 | 100 | 3400 | 200 | raj | PROFIT | 2017-05-06 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 3 | TN22BQ6226 | 3 | place one | 03:15 pm | place two | 06:15 pm | 1500 | 1800 | Cash | 300 | 1500| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
sorry for big question.
Too long for a comment...
Let's start by simplifying the problem as follows:
SELECT a.vid
, d.emp_id
, d.expcal
, f.entry_date
, f.entry_time
FROM add_vehicle a
JOIN fourth_table d
ON d.vid = a.vnumber
JOIN five_table f
ON f.vid = a.vnumber
vid emp_id expcal entry_date entry_time
1 3 100 2017-05-05 04:35:21
1 3 50 2017-05-05 04:35:21
1 3 100 2017-05-06 04:36:58
1 3 50 2017-05-06 04:36:58
http://sqlfiddle.com/#!9/62f3e5/21
Considering only the columns provided above, how does this differ from the desired result?
Incidentally - always store dates and times as a single entity
I'm trying to get the following result in mysql with below table: I
have tried to use sum (case when) but it give me the result for an
individual name only ,I want in the report the the total cost for each
user.
note: the ID column is not important you can ignore it .
mysql> select * from calls_records;
+----+-------+------------+------+
| id | month | name | cost |
+----+-------+------------+------+
| 1 | 1 | osama | 40 |
| 2 | 1 | rahman | 40 |
| 3 | 1 | ahmed | 30 |
| 4 | 1 | ali albann | 10.5 |
| 5 | 2 | osama | 10 |
| 6 | 2 | ali albann | 30 |
| 7 | 2 | ahmed | 10 |
| 8 | 2 | rahman | 10 |
+----+-------+------------+------+
expected result
+-----------+---------------------------+
| name | total_cost_for_each_user |
+------------+---------------------------+
| ahmed | 50 |
| ali albann | 40.5 |
| osama | 50 |
| rahman | 50 |
+------------+---------------------------+
query
select name, sum(cost) as totalcost
from calls_records
group by name
;
output
+------------+-----------+
| name | totalcost |
+------------+-----------+
| ahmed | 40 |
| ali albann | 40.5 |
| osama | 50 |
| rahman | 50 |
+------------+-----------+
sqlfiddle
I have three tables, mess_stock, mess_voucher, add_grocery.
Mess_stock table is below,
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| 49 | 5 | 4 | 100 | 10 | 100 | 10 | 10.00 | 1 |
| 50 | 17 | 5 | 111 | 10 | 20 | 101 | 60.00 | 1 |
| 51 | 7 | 3 | 123 | 2 | 1 | 124 | 300.00 | 1 |
| 52 | 7 | 1 | 123 | 20 | 20 | 123 | 500.00 | 2 |
| 53 | 14 | 8 | 100 | 5 | 95 | 10 | 60.00 | 2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
Mess_voucher table is below
+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
| 5 | VG1001 | 2015-02-19 |
| 6 | VG1001 | 2015-02-20 |
| 7 | VG1002 | 2015-02-20 |
| 8 | VG1002 | 2015-02-19 |
| 9 | MS1001 | 2015-02-20 |
| 10 | VG10012 | 2015-02-19 |
| 11 | 0 | 2015-02-23 |
| 12 | 1 | 2015-02-24 |
| 13 | MS1001 | 2015-02-25 |
| 14 | MS1001 | 2015-02-28 |
| 15 | VG1003 | 2015-02-28 |
| 16 | MS1001 | 2015-02-19 |
| 17 | MS1001 | 2015-02-21 |
+------------+--------------+--------------+
Add_grocery table is below
+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
| 1 | Rice | 25.00 |
| 3 | Mango | 150.00 |
| 4 | Coconut | 22.00 |
| 5 | Banana | 6.00 |
| 6 | Raddish | 12.00 |
| 7 | Apple | 150.00 |
| 8 | Pumkin | 12.00 |
+-----+-----------------+------------------+
I want to group the sum of pay_amount of mess_stock table. I have used the below query
SELECT opening_balance AS ope_stock,
balance AS clo_stock,
SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
SUM(IF(pay_type=2, pay_amount, 0)) est_pay
FROM mess_stock;
That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.
Try this it will work :
Use Inner Join :
SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC
+--------+---------+-------+--------+
| billID | orderId | price | date |
+--------+---------+-------+--------+
| 1 | 1 | 100 | 1.3.12 |
| 2 | 1 | 230 | 1.4.12 |
| 3 | 1 | 300 | 1.5.12 |
| 4 | 2 | 1000 | 1.3.12 |
| 5 | 2 | 160 | 1.4.12 |
| 6 | 3 | 400 | 1.3.12 |
+--------+---------+-------+--------+
I want to create view that have column that sum all price have same orderID but with date earlier than rows date. Like this:
+--------+---------+-------+--------+--------------+
| billID | orderId | price | date | add-on price |
+--------+---------+-------+--------+--------------+
| | | | | |
| 1 | 1 | 100 | 1.3.12 | 100 |
| 2 | 1 | 230 | 1.4.12 | 330 |
| 3 | 1 | 300 | 1.5.12 | 630 |
| 4 | 2 | 1000 | 1.3.12 | 1000 |
| 5 | 2 | 160 | 1.4.12 | 1160 |
| 6 | 3 | 400 | 1.3.12 | 400 |
+--------+---------+-------+--------+--------------+
You can user a correlated subquery for this:
select t.*,
(select sum(t2.price)
from table t2
where t2.orderId = t.orderId and t2.date <= t.date
) as CumulativePrice
from table t;