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.
Related
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
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
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;
I have a table called orders where I store information about orders made by an User.
order_id | user_id | amount
+++++++++++++++++++++++++++++++++++
1 | 1 | 100
2 | 1 | 200
3 | 2 | 200
4 | 3 | 100
5 | 3 | 100
6 | 4 | 500
What I want as final outcome is,
Number of Orders made by an user and total value of those orders.
So in above case output should look like,
user_id | count | sum
+++++++++++++++++++++++++++++++++++
1 | 2 | 300
2 | 1 | 200
3 | 3 | 600
4 | 1 | 500
What you need here is, just a GROUP BY with COUNT and SUM like so:
SELECT
user_id,
COUNT(User_id) Count,
SUM(amount) Sum
FROM Orders
GROUP BY user_id;
SQL Fiddle Demo
This will give you:
| USER_ID | COUNT | SUM |
-------------------------
| 1 | 2 | 300 |
| 2 | 1 | 200 |
| 3 | 2 | 200 |
| 4 | 1 | 500 |
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