Getting a total from multiple rows in MySQL - 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;

Related

How to select one row per user conditionally?

I have the following query:
select count(1) num, business_id, user_id FROM `pos_transactions`
group by user_id, business_id
order by user_id
It returns this:
+--------+-------------+---------+
| num | business_id | user_id |
+--------+-------------+---------+
| 3 | 503 | 12 |
| 7 | 33 | 12 |
| 1 | 771 | 13 |
| 2 | 86 | 13 |
| 1 | 772 | 13 |
| 4 | 652 | 14 |
| 4 | 567 | 14 |
+--------+-------------+---------+
I need to select only one row per user_id, the one which has a bigger num value. If all num values for a user are identical, then just one of them should be selected randomly (i.e. user #14). So, here is the expected result:
+--------+-------------+---------+
| num | business_id | user_id |
+--------+-------------+---------+
| 7 | 33 | 12 |
| 2 | 86 | 13 |
| 4 | 567 | 14 |
+--------+-------------+---------+
Any idea how can I do that?
I guess the solution will be something related to limit 1 per user. But I have no idea how I should write the query.
All I want to do is making the table unique per user_id, and the logic is selecting rows that have bigger num.
Use MAX() and FIRST_VALUE() window functions:
SELECT DISTINCT
MAX(COUNT(*)) OVER (PARTITION BY user_id) num,
FIRST_VALUE(business_id) OVER (PARTITION BY user_id ORDER BY COUNT(*) DESC) business_id,
user_id
FROM pos_transactions
GROUP BY user_id, business_id
ORDER BY user_id

MySQL SUM values from the same column if their id match

I would like to query a table where I would like to sum values from the same column / columns if their IDs match.
The table looks like this:
id | product_id | attribute_id | production_price | warehouse_id | qty | sell_qty
----------------------------------------------------------------
1 | 1 | 111 | 100.0000 | 1 | 50 | 45
2 | 1 | 111 | 100.0000 | 2 | 40 | 40
3 | 2 | 222 | 100.0000 | 1 | 30 | 20
4 | 2 | 222 | 100.0000 | 2 | 20 | 20
What I'm trying to do is sum the values of qty and sell_qty if the same products are in both warehuse(warehouse_id 1/2) and where product_id and attribute_id matches.
Something like this as a result:
product_id | attribute_id | production_price | qty | sell_qty
----------------------------------------------------------------
1 | 111 | 100.0000 | 90 | 85
2 | 222 | 100.0000 | 50 | 40
How can I do a query like this if its possible?
It looks like you want a simple group by:
select product_id, attribute_id, production_price, sum(qty) qty, sum(sell_qty) sell_qty
from mytable
group by 1,2,3
Try this :
SELECT sum(qty),sum(sell_qty) from table where warehouse_id = 1 group by product_id;
so it will sum the qty and sell-qty if there is a sale of same product under same warehouse
if you want to add more fields in select add the same in the group by too

MySQL compare between sum of two fields with group by statement

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;

count records by grouping of two columns

I am trying to count how many variations of amount there are per fk_id. If there is more than one variation then I want the record to be returned otherwise I want the record to be ignored.
What is wrong with the below statement?
SELECT *, count(fk_id) AS `count` FROM table
WHERE count > 1
GROUP BY fk_id, amount;
Table:
+-----------+----------+--------------+
| id | fk_id | amount |
+-----------+----------+--------------+
| 1 | 100 | 5 |
| 2 | 200 | 10 |
| 3 | 200 | 10 |
| 4 | 200 | 10 |
| 5 | 200 | 15 |
+-----------+----------+--------------+
Expected output:
+-----------+----------+--------------+--------------+
| id | fk_id | amount | count |
+-----------+----------+--------------+--------------+
| 2 | 200 | 10 | 2 |
+-----------+----------+--------------+--------------+
You cannot use an aggregate function in WHERE clause. Use HAVING clause instead:
SELECT MIN(id) as id,fk_id,amount, count(fk_id)-1 AS `count`
FROM table
GROUP BY fk_id, amount
HAVING count(fk_id)>1
Result:
ID FK_ID AMOUNT COUNT
2 200 10 2
See result in SQL Fiddle.

Mysql query to find sum of fields with same column value

I have a table like this
id | invent_id | order
1 | 95948214 | 70
2 | 46018572 | 30
3 | 46018572 | 20
4 | 46018572 | 50
5 | 36025764 | 60
6 | 36025764 | 70
7 | 95948214 | 80
8 | 95948214 | 90
I want get the sum of order qty with same invent id
That is the want the result like this
| invent_id | order
| 95948214 | 240
| 46018572 | 100
| 36025764 | 130
how can we write the mysql query
Make use of Aggregate function SUM and grouped them according to invent_id.
SELECT invent_id, SUM(`order`) `Order`
FROM tableName
GROUP BY invent_ID
GROUP BY clause
SQLFiddle Demo