Get the required sql query result - mysql

mysql> select calendar.calId,calendar.cdate,lsUsers.userId,
apptActual.OPDID,count(*)
from calendar
LEFT JOIN apptActual on calendar.calId=apptActual.calendarId
LEFT JOIN lsUsers on lsUsers.userId=apptActual.OPDID
group by 1,2,3,4 order by 3
desc limit 10;
Result is:
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | NULL | NULL | 1 |
| 1287 | 2019-07-10 | NULL | NULL | 1 |
| 1295 | 2019-07-18 | NULL | NULL | 1 |
| 1303 | 2019-07-26 | NULL | NULL | 1 |
| 1311 | 2019-08-03 | NULL | NULL | 1 |
+-------+------------+--------+-------+----------+
10 rows in set (0.34 sec)
By using above query I am getting result as above, but I need result result like below (all userId's has to come.)
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | 4 | NULL | 1 |
| 1287 | 2019-07-10 | 21| NULL | 1 |
| 1295 | 2019-07-18 | 3 | NULL | 1 |
| 1303 | 2019-07-26 | 7 | NULL | 1 |
| 1311 | 2019-08-03 | 5 | NULL | 1 |
+-------+------------+--------+-------+----------+
Please suggest me how to get result as above!

If you join lsUsers on userId = appActual.OPDID you will get what you ask for: usedId (NULL) = OPDID (NULL). Is there any other way to join lsUsers?

Related

SQL Where SUM() Between two values

I am trying to find account info for which the sum of available_balance is between two values. I tried to the following but it is not working as expected:
SELECT sum(a.avail_balance) `sum`
FROM account a
WHERE `sum` BETWEEN 5000 AND 10000
Result:ERROR 1054 (42S22): Unknown column 'sum' in 'where clause'
How can I accomplish my intended result?
My table:
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
You can only use aggregates for comparison in the HAVING clause:
GROUP BY ...
HAVING SUM(avail_balance) BETWEEN 5000 AND 10000
The HAVING clause requires you to define a GROUP BY clause.
Assuming, you want to SUM the balance for each cust_id, you need something like
SELECT sum(a.avail_balance) `sum`
FROM account a
GROUP BY `cust_id`
HAVING SUM(a.avail_balance) BETWEEN 5000 AND 10000
SELECT sum(a.avail_balance)'sum'
FROM account a
WHERE a.avail_balance between 5000 and 10000
Aggregate functions(AVG,COUNT,MIN,MAX,SUM) for comparison can be achieved by using the "HAVING" clause which requires "GROUP BY" clause(relevant category column depending on the requirement).
As rightly pointed by Pankaj Gadge, cust_id is the most suitable for the requirement of the problem statement
SELECT SUM(a.avail_balance) 'Sum'
FROM account a
GROUP BY cust_id
HAVING SUM(a.avail_balance) BETWEEN 5000 AND 10000;
aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause
ex: group by column_name
having sum(avail_balance) between 5000 and 10000

not able to fetch date value using subqueries mysql

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;

MYSQL GROUP BY HAVING clause unsing MIN and MAX for a range

The query:
SELECT product_cd, SUM(avail_balance) prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
HAVING MIN(avail_balance) >= 1000
AND MAX(avail_balance) <= 10000;
The table:
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
Result:
+------------+--------------+
| product_cd | prod_balance |
+------------+--------------+
| CD | 19500.00 |
| MM | 17045.14 |
+------------+--------------+
My understanding goes as far as this, from the query above:
SELECT product_cd, SUM(avail_balance) prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
Which results in:
+------------+--------------+
| product_cd | prod_balance |
+------------+--------------+
| BUS | 9345.55 |
| CD | 19500.00 |
| CHK | 73008.01 |
| MM | 17045.14 |
| SAV | 1855.76 |
| SBL | 50000.00 |
+------------+--------------+
My confusion is with:
HAVING MIN(avail_balance) >= 1000
AND MAX(avail_balance) <= 10000;
Which I interpret as: "Include only the ones having a balance that is greater than or equal to 1,000 and lesser than or equal to 10,000" which would result in
BUS: 9345.55
SAV: 1855.76
Which is completely wrong when I ran the query as above. Any explanations as to what is going on would help.
instead of MIN(avail_balance) and MIN(avail_balance), try
HAVING MIN(SUM(avail_balance)) >=1000
and MAX(SUM(avail_balance)) <=10000
You are getting CD and MM in the result beacause all of their values are in between 1000 to 10000
Do you want the total or the minimum or maximum avail_balance match a certain range? They are different things.
The product CD for instance has a total of 19500, a minimum of 1500 and a maximum of 10000. You are showing the total (SUM), but want to see results where the product's minimum (MIN) is >= 1000 and the product's maximum (MAX) is <= 10000. This is true for CD.
So your query shows the correct results.
Maybe you want these, or maybe you want
SELECT
product_cd,
SUM(avail_balance) AS prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
HAVING SUM(avail_balance) >= 1000
AND SUM(avail_balance) <= 10000;
instead.

MySQL SUM() returning spurious (?) values

Despite spending an hour on this, the solution is eluding me still. I have a complex-ish query that is returning incorrect data for the SUM(). Yet, when I strip it down to the barest form, it outputs the correct data. But why and fix, I cannot figure out.
The Problem
SELECT po.*, SUM( poo.material_qty ) AS total_items_ordered, suppliers.supplier_name
FROM `purchase_orders` po
LEFT JOIN purchase_orders_items poo ON poo.poid = po.poid
LEFT JOIN suppliers ON suppliers.supplier_id = po.supplier_id
LEFT JOIN materials_batch mb ON mb.purchase_order_no = po.poid
WHERE po_status NOT
IN (
'Fulfilled', 'Cancelled'
)
AND batch_status NOT
IN (
'Arrived', 'Cancelled', 'Refused', 'Missing', 'Damaged', 'Completed'
)
GROUP BY po.poid
ORDER BY date_expected ASC
Provides wildly incorrect data for 'total_items_ordered'.
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+---------------------+-----------------------+
| poid | date_raised | date_expected | supplier_id | job_id | job_item_id | ref_no | sub_total | VAT | total | userid | DN | manual | po_status | total_items_ordered | supplier_name |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+---------------------+-----------------------+
| 15571 | 2014-06-24 13:32:55 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 14850.10 | 2970.02 | 17820.12 | 1 | | N | Raised | 545 | John Parker & Son Ltd |
| 15572 | 2014-06-24 13:33:26 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 997.80 | 199.56 | 1197.36 | 1 | | N | Raised | 80 | John Parker & Son Ltd |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+---------------------+-----------------------+
2 rows in set (0.00 sec)
And yet, when I strip all the complexities out of the query and run the raw SUM(), the value is correct:
mysql> SELECT poid, SUM(material_qty) AS total_items_ordered FROM `purchase_orders_items` GROUP BY poid;
+-------+---------------------+
| poid | total_items_ordered |
+-------+---------------------+
| 15571 | 109 |
| 15572 | 20 |
+-------+---------------------+
2 rows in set (0.00 sec)
Can anyone shed any light on where I'm going wrong here?? I've included all the test table content below just in case you can spot something I've missed. Thank you!
Data Example
mysql> SELECT * FROM purchase_orders;
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+
| poid | date_raised | date_expected | supplier_id | job_id | job_item_id | ref_no | sub_total | VAT | total | userid | DN | manual | po_status |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+
| 15571 | 2014-06-24 13:32:55 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 14850.10 | 2970.02 | 17820.12 | 1 | | N | Raised |
| 15572 | 2014-06-24 13:33:26 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 997.80 | 199.56 | 1197.36 | 1 | | N | Raised |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM purchase_orders_items;
+--------+-------+-------------+--------------+----------------+--------------+--------------------------------------------------+
| poi_id | poid | material_id | material_qty | material_price | material_sku | material_name |
+--------+-------+-------------+--------------+----------------+--------------+--------------------------------------------------+
| 1 | 15571 | 2 | 3 | 100.00 | PKS275282 | 406x140 White Universal Beam (S355) |
| 2 | 15571 | 5 | 10 | 17.40 | 118-64-44 | Test Item (S275) |
| 3 | 15571 | 8 | 1 | 9984.50 | 113-64-21 | A really really really big universal beam (S355) |
| 4 | 15571 | 9 | 77 | 10.00 | 12345 | A thing |
| 5 | 15571 | 10 | 18 | 201.20 | 12-34-56 | 102x230 Narrow Beam (S355) |
| 6 | 15572 | 2 | 6 | 100.00 | PKS275282 | 406x140 White Universal Beam (S355) |
| 7 | 15572 | 5 | 9 | 17.40 | 118-64-44 | Test Item (S275) |
| 8 | 15572 | 9 | 4 | 10.00 | 12345 | A thing |
| 9 | 15572 | 10 | 1 | 201.20 | 12-34-56 | 102x230 Narrow Beam (S355) |
+--------+-------+-------------+--------------+----------------+--------------+--------------------------------------------------+
9 rows in set (0.00 sec)
mysql> SELECT * FROM suppliers;
+-------------+-----------------------+--------------------+--------------+---------------------+-------------------+-----------------------+--------------------------+---------------------+----------------------+
| supplier_id | supplier_name | supplier_telephone | supplier_fax | supplier_added_date | supplier_added_by | supplier_last_updated | supplier_last_updated_by | supplier_assessed | supplier_approved_by |
+-------------+-----------------------+--------------------+--------------+---------------------+-------------------+-----------------------+--------------------------+---------------------+----------------------+
| 1 | John Parker & Son Ltd | 01227 783333 | 0800 521932 | 2014-05-04 15:57:43 | 1 | 2014-06-05 16:38:23 | 1 | 2014-05-04 15:57:43 | 2 |
| 2 | Superior Glass Ltd. | 01825 764766 | 01825 767699 | 2014-05-04 17:48:38 | 1 | 2014-06-04 20:14:16 | 1 | 2014-05-04 17:48:38 | 3 |
| 3 | DTS Origins Ltd. | 01283 3283029 | 01928 303494 | 2014-05-04 17:51:57 | 1 | 2014-05-04 17:53:08 | 1 | 2014-05-04 17:51:57 | 2 |
+-------------+-----------------------+--------------------+--------------+---------------------+-------------------+-----------------------+--------------------------+---------------------+----------------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM materials_batch;
+-------------------+-------+---------------------+-------------------+------------------+-----+---------+------------+-------------+-------------+--------------+
| material_batch_id | poiid | rcvd_date | purchase_order_no | delivery_note_no | qty | rcvd_by | dn_scanned | material_id | supplier_id | batch_status |
+-------------------+-------+---------------------+-------------------+------------------+-----+---------+------------+-------------+-------------+--------------+
| 1 | 1 | 0000-00-00 00:00:00 | 15571 | | 3 | 0 | No | 2 | 1 | Ordered |
| 2 | 2 | 0000-00-00 00:00:00 | 15571 | | 10 | 0 | No | 5 | 1 | Ordered |
| 3 | 3 | 0000-00-00 00:00:00 | 15571 | | 1 | 0 | No | 8 | 1 | Ordered |
| 4 | 4 | 0000-00-00 00:00:00 | 15571 | | 77 | 0 | No | 9 | 1 | Ordered |
| 5 | 5 | 0000-00-00 00:00:00 | 15571 | | 18 | 0 | No | 10 | 1 | Ordered |
| 6 | 6 | 0000-00-00 00:00:00 | 15572 | | 6 | 0 | No | 2 | 1 | Ordered |
| 7 | 7 | 0000-00-00 00:00:00 | 15572 | | 9 | 0 | No | 5 | 1 | Ordered |
| 8 | 8 | 0000-00-00 00:00:00 | 15572 | | 4 | 0 | No | 9 | 1 | Ordered |
| 9 | 9 | 0000-00-00 00:00:00 | 15572 | | 1 | 0 | No | 10 | 1 | Ordered |
+-------------------+-------+---------------------+-------------------+------------------+-----+---------+------------+-------------+-------------+--------------+
The reason for the wrong results should be clear when you leave out the GROUP BY from your query. For each table you JOIN, the number of returned rows is multiplied by the number of rows found by the JOIN.
As the materials_batch table contains multiple entries per order, the resulting total_items_ordered is multiplied by 5 for order number 15571, and its multiplied by 4 for order number 15572.
Try the following:
SELECT
po.*,
(
SELECT SUM(poo.material_qty)
FROM purchase_orders_items poo
WHERE poo.poid = po.poid
) AS total_items_ordered,
suppliers.supplier_name
FROM `purchase_orders` po
LEFT JOIN suppliers ON suppliers.supplier_id = po.supplier_id
LEFT JOIN materials_batch mb ON mb.purchase_order_no = po.poid
WHERE po_status NOT
IN (
'Fulfilled', 'Cancelled'
)
AND batch_status NOT
IN (
'Arrived', 'Cancelled', 'Refused', 'Missing', 'Damaged', 'Completed'
)
GROUP BY po.poid
ORDER BY date_expected ASC

compare two tables's data in MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
There are two tables
Table 1
select * from manage_tcp;
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 2 | 1433 | up |
| 2 | 3389 | up |
| 3 | 3389 | down |
| 3 | 1433 | down |
| 9 | 3389 | up |
| 8 | 3389 | up |
| 7 | 1433 | up |
| 6 | 3389 | up |
| 5 | 3389 | up |
| 4 | 3389 | up |
| 10 | 1433 | up |
| 11 | 1433 | up |
| 12 | 3389 | up |
| 13 | 1433 | up |
| 14 | 3389 | up |
| 15 | 1433 | up |
| 16 | 3389 | up |
| 17 | 1433 | up |
| 18 | 3389 | up |
| 19 | 1433 | up |
| 20 | 3389 | up |
| 21 | 1433 | up |
| 24 | 1433 | up |
| 23 | 3389 | up |
| 25 | 3389 | up |
| 26 | 1433 | up |
| 29 | 1433 | up |
| 28 | 3389 | up |
| 30 | 3389 | up |
| 31 | 80 | up |
| 32 | 3389 | up |
| 33 | 80 | up |
| 34 | 3389 | up |
| 35 | 80 | up |
+----+----------+--------+
34 rows in set (0.00 sec)
Table 2
mysql> select * from manage_host;
+----+------------+-------------+--------+-------+--------------+----------+------+
| id | uptime | type | statut | group | thresold_ref | thresold | mail |
+----+------------+-------------+--------+-------+--------------+----------+------+
| 2 | 277248529 | win2003.png | up | 4 | 1 | 0 | NULL |
| 3 | 277277471 | win2003.png | prob | 4 | 1 | 0 | NULL |
| 4 | 346159833 | win2003.png | up | 4 | 1 | 0 | NULL |
| 5 | 930205491 | win2003.png | up | 5 | 1 | 0 | NULL |
| 6 | 3805663007 | win2003.png | up | 5 | 1 | 0 | NULL |
| 7 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 8 | 29413867 | win2003.png | up | 5 | 1 | 0 | NULL |
| 9 | 981986401 | win2003.png | up | 5 | 1 | 0 | NULL |
| 10 | -1 | win2003.png | up | 5 | 1 | 0 | NULL |
| 11 | -1 | win2003.png | up | 5 | 1 | 0 | NULL |
| 12 | 2230787611 | win2003.png | up | 7 | 1 | 0 | NULL |
| 13 | -1 | win2003.png | up | 7 | 1 | 0 | NULL |
| 14 | 1004161923 | win2003.png | up | 8 | 1 | 0 | NULL |
| 15 | -1 | win2003.png | up | 8 | 1 | 0 | NULL |
| 16 | 1592294954 | win2003.png | up | 8 | 1 | 0 | NULL |
| 17 | -1 | win2003.png | up | 8 | 1 | 0 | NULL |
| 18 | 1449216250 | win2003.png | up | 4 | 1 | 0 | NULL |
| 19 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 20 | 3461945234 | win2003.png | up | 4 | 1 | 0 | NULL |
| 21 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 23 | 3461946968 | win2003.png | up | 4 | 1 | 0 | NULL |
| 24 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 25 | 3462022562 | win2003.png | up | 4 | 1 | 0 | NULL |
| 26 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 35 | 3318333401 | win2003.png | up | 6 | 1 | 0 | |
| 28 | 3461879984 | win2003.png | up | 4 | 1 | 0 | NULL |
| 29 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 30 | 1872950109 | win2003.png | up | 6 | 1 | 0 | NULL |
| 31 | 1872950125 | win2003.png | up | 6 | 1 | 0 | NULL |
| 32 | 1955232033 | win2003.png | up | 6 | 1 | 0 | NULL |
| 34 | 3318333401 | win2003.png | up | 6 | 1 | 0 | |
| 33 | 1955232049 | win2003.png | up | 6 | 1 | 0 | NULL |
+----+------------+-------------+--------+-------+--------------+----------+------+
32 rows in set (0.00 sec)
I'd like to compare those two tables above and to get the results in table 1 but not in table 2 like: (this is the result i want)
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 2 | 1433 | up |
| 3 | 3389 | down |
| 3 | 1433 | down |
+----+----------+--------+
I've tried
SELECT a.*,b.*
FROM manage_tcp as a
LEFT JOIN manage_host as b
USING(id,statut)
WHERE b.id is null;
but I got
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 3 | 3389 | down |
| 3 | 1433 | down |
+----+----------+--------+
it's not what I want.
Thanks a lot!
Try this:
SELECT m.id, m.services, m.statut
FROM manage_tcp m
WHERE EXISTS
(
SELECT *
FROM manage_host mh
WHERE mh.id = m.id AND m.statut = mh.statut
)
This will give you the intersection of the tables manage_tcp and manage_host based on id and status columns.
get the results in table 1 but not in table 2
If you need the difference between the tow tables, use NOT EXISTS instead of EXISTS like this:
SELECT m.id, m.services, m.statut
FROM manage_tcp m
WHERE NOT EXISTS
(
SELECT *
FROM manage_host mh
WHERE mh.id = m.id AND m.statut = mh.statut
)
Typically, this is the implementations of the INTERSECT and EXCEPT operators which are defined by the SQL standard as the set intersection and difference operators. But, unfortunately, most database system doesn't implement these operators. MS SQL Server does support these operators but not MYSQL .
Try this: (assume the comparison column are id and statut)
SELECT a.id, a.services, a.statut
FROM manage_tcp a
WHERE NOT EXISTS(SELECT b.id FROM manage_host b WHERE b.id=a.id AND b.statut=a.statut)
UNION
SELECT a.id, (SELECT services FROM manage_tcp WHERE id=a.id ORDER BY id LIMIT 1), a.statut
FROM manage_tcp a
GROUP BY a.id, a.statut HAVING COUNT(1)>1
The first query will produce result from table a where not in table b.
The second, after UNION, will produce result for id that appear twice in table a.
SELECT manage_tcp.* FROM
(
SELECT a.id,a.statut,a.cnt
FROM (
SELECT id, statut, COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id, a.statut
HAVING a.cnt > COUNT(1)
) AS diff
LEFT JOIN manage_tcp USING(id,statut)
or
SELECT a.id,a.statut,a.services,a.cnt
FROM (
SELECT id,statut,services,COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id,a.statut
HAVING a.cnt > COUNT(1)