+----------------+
| 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;
Related
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;
i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;
I have these two table.
-----------------------------------------------
|order_id | cust_id| order_detail| order_price|
|101 | 203 | Canon-XL | 4500.00 |
|102 | 201 | Canon-XL | 4500.00 |
|103 | 201 | Battery-L | 850.00 |
|104 | 207 | EPSONL 120 | 5100.00 |
|105 | 205 | EPSONL 120 | 5100.00 |
|106 | 203 | Battery-S | 5100.00 |
|107 | 204 | HP DESK-230 | 520.00 |
|108 | 206 | TRIPOD-XL | 1550.00 |
-----------------------------------------------
------------------------------
|cust_id| salary_id| salary |
|201 | 101 | 3200.00 |
|202 | 102 | 4100.00 |
|203 | 103 | 2000.00 |
|204 | 104 | 5100.00 |
|205 | 105 | 5100.00 |
|206 | 106 | 2500.00 |
|207 | 107 | 2700.00 |
------------------------------
Now I want to sum the order_price on tb1 where salary on tb2 is less than 3000..
How can I do this?
------------
|total_order |
|------------|
|11150 |
------------
You could join the two tables according to the cust_id:
SELECT SUM(order_price)
FROM tb1
JOIN tb2 ON tb1.cust_id = tb2.cust_id
WHERE salary < 3000
+--------+---------+-------+--------+
| 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;
I have this table:
+----------+-------------+----------+------+----------+
| suite_id | building_id | bedrooms | rent | occupied |
+----------+-------------+----------+------+----------+
| 745 | 1000 | 1 | 800 | 0 |
| 746 | 1000 | 1 | 810 | 1 |
| 747 | 1000 | 2 | 1000 | 0 |
| 748 | 1002 | 2 | 1010 | 0 |
| 749 | 1004 | 2 | 1020 | 1 |
| 750 | 1010 | 1 | 750 | 0 |
| 751 | 1020 | 2 | 950 | 0 |
| 752 | 1020 | 1 | 400 | 1 |
| 753 | 1020 | 2 | 1200 | 0 |
| 754 | 1020 | 5 | 3000 | 0 |
| 755 | 1030 | 2 | 1100 | 0 |
| 760 | 1006 | 1 | 500 | 1 |
| 761 | 1006 | 2 | 900 | 1 |
| 762 | 1006 | 2 | 950 | 1 |
| 770 | 1180 | 2 | 990 | 1 |
| 771 | 1180 | 1 | 600 | 1 |
| 772 | 1180 | 2 | 900 | 0 |
| 773 | 1170 | 3 | 1100 | 1 |
| 774 | 1170 | 2 | 1000 | 1 |
| 780 | 1160 | 1 | 200 | 1 |
| 781 | 1160 | 2 | 400 | 1 |
| 782 | 1004 | 2 | 1100 | 1 |
| 783 | 1004 | 2 | 1111 | 1 |
+----------+-------------+----------+------+----------+
now what I need to do is check if any buildings aren't occupied, which would be just 1030 (I think). I believe I need to check each building id, see if it's ever occupied(set to 1), and if it isn't then have that returned. this is an assignment, and we've just began working with mysql, so try to keep it relatively simple, thanks.