MySQL compare between sum of two fields with group by statement - mysql

Below from the table, I am expecting those invoice's all item are
packed.
Table:
mysql> select * from allotment;
+----+------------+---------+-----------+------------+------------+
| id | invoice_id | item_id | total_qty | packed_qty | created |
+----+------------+---------+-----------+------------+------------+
| 1 | 4 | 26 | 4 | 4 | 2016-08-31 |
| 2 | 4 | 38 | 1 | 1 | 2016-08-31 |
| 3 | 5 | 39 | 16 | 8 | 2016-08-31 |
| 4 | 5 | 2 | 2 | 5 | 2016-08-31 |
+----+------------+---------+-----------+------------+------------+
My query:
mysql> SELECT invoice_id, created FROM allotment
where sum(allotment.total_qty)=sum(allotment.packed_qty)
GROUP BY invoice_id;
[**ERROR 1111 (HY000): Invalid use of group function]
I have applied many way but it didn't work. Actually I need to compare
"sum of total_qty" and "sum of packed_qty" against same
"invoice_id".
My Expected result:
+------------+------------+
| invoice_id | created |
+------------+------------+
| 4 | 2016-08-31 |
Logic: Invoice_id 4, total_item = 4+1 and total_packed= 4+1
[select where total_item==total_packed]
Is there any way to get this result from the "allotment" table?

You need to use HAVING, not WHERE
SELECT invoice_id, created,sum(allotment.total_qty) t
sum(allotment.packed_qty) p
FROM allotment
GROUP BY invoice_id
Having t=p;
MySQL HAVING clause to specify a filter condition for groups of rows or aggregates.

Try this
SELECT a1.invoice_id, a1.created FROM allotment AS a1
WHERE (SELECT SUM(a2.total_qty) FROM allotment AS a2 WHERE a2.invoice_id = a1.invoice_id) = (SELECT SUM(a2.packed_qty) FROM allotment AS a2 WHERE a2.invoice_id = a1.invoice_id)
GROUP BY invoice_id;

Related

Mysql delete similar rows according to specific columns except the ones with highest id

my table has duplicate row values in specific columns. i would like to remove those rows and keep the row with the latest id.
the columns i want to check and compare are:
sub_id, spec_id, ex_time
so, for this table
+----+--------+---------+---------+-------+
| id | sub_id | spec_id | ex_time | count |
+----+--------+---------+---------+-------+
| 1 | 100 | 444 | 09:29 | 2 |
| 2 | 101 | 555 | 10:01 | 10 |
| 3 | 100 | 444 | 09:29 | 23 |
| 4 | 200 | 321 | 05:15 | 5 |
| 5 | 100 | 444 | 09:29 | 8 |
| 6 | 101 | 555 | 10:01 | 1 |
+----+--------+---------+---------+-------+
i would like to get this result
+----+--------+---------+---------+-------+
| id | sub_id | spec_id | ex_time | count |
+----+--------+---------+---------+-------+
| 5 | 100 | 444 | 09:29 | 8 |
| 6 | 101 | 555 | 10:01 | 1 |
+----+--------+---------+---------+-------+
i was able to build this query to select all duplicate rows from multiple columns, according to this question
select t.*
from mytable t join
(select id, sub_id, spec_id, ex_time, count(*) as NumDuplicates
from mytable
group by sub_id, spec_id, ex_time
having NumDuplicates > 1
) tsum
on t.sub_id = tsum.sub_id and t.spec_id = tsum.spec_id and t.ex_time = tsum.ex_time
but now im not sure how to wrap this select with a delete query to delete the rows except for the ones with highest id.
as shown here
You can modify your sub-select query, to get maximum value of id for each duplication combination.
Now, while joining to the main table, simply put a condition that id value will not be equal to the maximum id value.
You can now Delete from this result-set.
Try the following:
DELETE t
FROM mytable AS t
JOIN
(SELECT MAX(id) as max_id,
sub_id,
spec_id,
ex_time,
COUNT(*) as NumDuplicates
FROM mytable
GROUP BY sub_id, spec_id, ex_time
HAVING NumDuplicates > 1
) AS tsum
ON t.sub_id = tsum.sub_id AND
t.spec_id = tsum.spec_id AND
t.ex_time = tsum.ex_time AND
t.id <> tsum.max_id

Getting a total from multiple rows in MySQL

I am not very good at MySQL queries. Can someone help me figure out how to do this?
I have a table like this (lets call it stats):
+----+-------+-----+
| id | memid | qty |
+----+-------+-----+
| 1 | 99 | 0 |
+----+-------+-----+
| 2 | 102 | 22 |
+----+-------+-----+
| 3 | 102 | 10 |
+----+-------+-----+
| 4 | 99 | 100 |
+----+-------+-----+
| 5 | 17 | 25 |
+----+-------+-----+
| 6 | 87 | 72 |
+----+-------+-----+
| 7 | 36 | 0 |
+----+-------+-----+
| 8 | 102 | 6 |
+----+-------+-----+
I need a MySQL query that will combine the qty of all the memids and ORDER BY ASC the total qty for each memid.
Thank you in advance for your help! :)
You can select SUM as another field in query and order it by qty, e.g.:
SELECT id, memid, qty, SUM(qty)
FROM table
ORDER BY qty;
Please note that SUM will return the same value for all the rows as it will be a constant value.
If you have multiple records per memid and want to calculate SUM per memid then you can use GROUP BY e.g.:
SELECT memid, SUM(qty) AS `sum`
FROM table
GROUP BY memid
ORDER BY sum;

SQL select count from multiple tables

I'm a starter at SQL and I have the following tables, ORDER_PRODUCTS, listing the products of an order and EXCHANGE_PRODUCTS, listing products that will be exchanged.
Both have the same fields, and I need to make a selection counting the amount of products in both tables, distinguishing them by the order_id, does anyone knows how I can do this?
ORDER_PRODUCTS
+-----+------------+----------+---------+
| id | product_id | order_id | amount |
+-----+------------+----------+---------+
| 1 | 5 | 1 | 2 |
| 2 | 7 | 1 | 1 |
| 3 | 13 | 5 | 1 |
| 4 | 18 | 8 | 3 |
| 5 | 45 | 11 | 4 |
+-----+------------+----------+---------+
EXCHANGE_PRODUCTS
+-----+------------+----------+---------+
| id | product_id | order_id | amount |
+-----+------------+----------+---------+
| 1 | 5 | 1 | 1 |
| 2 | 7 | 1 | 2 |
| 3 | 13 | 5 | 1 |
| 4 | 3 | 8 | 2 |
| 5 | 2 | 11 | 1 |
+-----+------------+----------+---------+
You want to use union all to combine the tables and then aggregate them. I might recommend:
select order_id, sum(ordered) as ordered, sum(exchanged) as exchanged,
sum(exchanged + ordered) as total
from ((select order_id, amount as ordered, 0 as exchanged
from order_products
) union all
(select order_id, 0 as ordered, amount as exchanged
from exhange_products
)
) oe
group by order_id;
It is important to use union all rather than union, because union removes duplicates (which can result in bad numbers). Union also incurs overhead that is unnecessary.
And, by "count amount" I assume you really mean to take the sum.
I think this query should do what you Need:
select sum(amount), order_id from (
select amount,order_id from order_products
union
select amount,order_id from Exchange_products)
group by order_id

MySQL query to select only entries where value = X and value <> Y

Raw MySQL queries are absolutely not my forte, so I'm struggling with this a bit, but: with a straightforward table layout like this:
+----+-----------+----------+---------------------+
| id | status_id | order_id | created_at |
+----+-----------+----------+---------------------+
| 1 | 1 | 1 | 2016-03-21 20:40:39 |
| 2 | 3 | 1 | 2016-03-21 20:40:45 |
| 3 | 5 | 1 | 2016-03-21 20:47:14 |
| 4 | 1 | 2 | 2016-03-25 12:14:44 |
| 6 | 3 | 2 | 2016-03-25 12:16:12 |
| 7 | 5 | 2 | 2016-03-25 12:47:43 |
| 8 | 1 | 3 | 2016-03-26 17:25:12 |
| 9 | 3 | 3 | 2016-03-26 17:25:48 |
+----+-----------+----------+---------------------+
I want to select only the order_id rows where the status_id equals 3, but not where that same order_id has a status_id of 5. As a result, my query should only return order ID 3, but my current query returns all 3 order IDs in the results:
$statusQueryString = 'SELECT DISTINCT order_id
FROM shop_order_status_log_records
WHERE status_id = 3 AND status_id <> 5 ORDER BY created_at';
Where am I going wrong with my query?
Use post aggregate filtering when you need 2 or more conditions per group.A simple rule WHERE filters rows HAVING filters groups
SELECT order_id FROM shop_order_status_log_records
GROUP BY order_id
HAVING SUM(status_id = 3)>0
AND SUM(status_id = 5)=0

Sum up values in SQL once all values are available

I have events flowing into a MySQL database and I need to group and sum the events to transactions and store away into another table. The data looks like:
+----+---------+------+-------+
| id | transid | code | value |
+----+---------+------+-------+
| 1 | 1 | b | 12 |
| 2 | 1 | i | 23 |
| 3 | 2 | b | 34 |
| 4 | 1 | e | 45 |
| 5 | 3 | b | 56 |
| 6 | 2 | i | 67 |
| 7 | 2 | e | 78 |
| 8 | 3 | i | 89 |
| 9 | 3 | i | 90 |
+----+---------+------+-------+
The events arrive in batches and I would like to create the transaction by summing up the values for each transid, like:
select transid, sum(value) from eventtable group by transid;
but only after all the events for that transid have arrived. That is determined by the event with the code e (b for the beginning, e for the end and i for varying amount of intermediates). Being a novice in SQL, how could I implement the requirement for the existance of the end code before the summing?
Perhaps with having:
select transid, sum(value)
from eventtable
group by transid
having max(case code when 'e' then 1 end)=1;
select transid, sum(value) from eventtable
group by transid
HAVING COUNT(*) = 3
you should count the records in the group. So when there is (b)egin, (i)?? don't know what it is and (e)nd this group is not filtered out.