I have a table ORDERS which has something like this value ,
customerNumber | orderNumber(PK)
40 1
30 2
40 3
20 4
30 5
So, this table has customerNumbers 40 and 30 placing the max orders. Can anyone tell me a MySQL query to return the customerNumber (numbers), i dont want the count of the orders, just want the customer (cutomers) with the max order placed .
Thanks.
You can use below statement to get the Customer who placed maximum orders.
SELECT customerNumber FROM orders
GROUP BY customerNumber
ORDER BY COUNT(orderNumber) DESC LIMIT 1;
I should get deservedly flamed for this, but hey, the sun's out and it's feeling like a good day...
SELECT x.customernumber
FROM
( SELECT customernumber
, COUNT(*) total
FROM my_table
GROUP
BY customernumber
) x
JOIN
( SELECT COUNT(*) total
FROM my_table
GROUP
BY customernumber
ORDER
BY total DESC
LIMIT 1
) y
ON y.total = x.total;
Related
Im trying to get the mode (the value that appears most often in a set of data) of price for each store in the following table
create table t_products (
store_id int,
product varchar(20),
price int
)
I already have this query which retrieves all the ocurrences of each price in each store
SELECT store_id, price, count(price)
FROM t_products
GROUP BY store_id, price
ORDER BY count(price) DESC;
What else is missing? I was trying to use max() function to get the highest price for each store in several ways with no success.
Thanks in advance
PD this is the result of my current query.
store_id|price|count(price)
2 40 5
1 70 5
2 90 4
3 60 2
1 60 1
3 50 1
3 80 1
1 50 1
I only want to keep
store price
2 40
1 70
3 60
select tmp.store_id, tmp.price from (
SELECT store_id, price
FROM t_products
GROUP BY store_id, price
ORDER BY count(price) DESC
) as tmp
group by tmp.store_id
Good luck )
SELECT store_id, value
FROM (SELECT store_id, value, count(value) c
FROM t_products
GROUP BY store_id, value
ORDER BY count(value) DESC) ds
GROUP BY store_id
HAVING max(ds.c);
Forewarning ,I'm a total newbie so be gentle.I need to get the SUM of qty from a set of values and then the top 5 MAX sum qty from that list. But I need to leave out a NULL value from final list.
I would do this without a subquery as:
select game_name, sum(qty) AS total
from sales
where game_name is not null
group by game_name
order by total desc
limit 5;
Try this:
SELECT game_name,
max(total) AS total
FROM ( select game_name,
sum(qty) AS total
FROM sales
GROUP BY game_name
) AS RANK
where game_name is not null
group by game_name
order by total desc limit 5 ;
It will remove any null game, order by total descending and it gives you the first 5 rows.
I have a table "sales"
transactionId salepersonId amount
1 1 100
2 1 200
3 2 50
4 3 60
5 3 200
I like to find out how to get the salesperson with the highest total sale.
I know I need to use max and sum but I don't know how to combine them.
This is what I got
select salespersonId, sum(amount) from sales group by salesperson;
This would give me the total sales of each person, but I am not sure the next step to get the max. can someone help me ?
The standard SQL way is to use order by and limit or top
select salespersonid, sum(amount) as total
from sales
group by salespersonid
order by total desc
limit 1;
In SQL Server, you would use top 1 instead of limit 1:
select top 1 salespersonid, sum(amount) as total
from sales
group by salespersonid
order by total desc;
select salespersonId, sum(amount) as total
from sales group by salespersonID ORDER BY total DESC LIMIT 1;
This should work
select salepersonId, sum(amount) from sales group by amount DESC limit 1
OR
select rec.id, max(rec.amt) from (select salepersonId id, sum(amount) amt from sales group by salepersonId) AS rec
I have this table :
Orders(Orderid,CustomerID,Amount)
I ask id's of top 10 customers (total amount of order)
I wrote the query which is print all customer (highest amount to min amount)
select CustomerID, sum(Amount) as Total from orders group by CustomerID order by Total desc;
How can I get the first 10 line of this result ? Or is my way to obtain top 10 wrong?
Here is what you want:
select CustomerID, sum(Amount) as Total
from orders
group by CustomerID
order by Total desc
LIMIT 10;
I am trying to get a MYSql statement to spit out the most common number in a field. I believe I am supposed to use COUNT(QUANTITY) but I am confused by which to GROUP BY and ORDER BY, I can't seem to get the correct MODE (Most common number).
*EDIT*
Here is a sample table:
QUANTITY | ORDER_NUMBER
1 51541
4 12351
5 11361
5 12356
6 12565
8 51424
10 51445
25 51485
The MYSql statement should spit out the number 5 because it appears most often
SELECT QUANTITY,COUNT(*)
FROM ...
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
SELECT ORDER_NUMBER AS ORDER, COUNT(QUANTITY) as numorders
FROM table
GROUP BY ORDER_NUMBER
ORDER BY numorders
to get the top 10 order_numbers do
select order_number, count(order_number) as quantity
from your_table
group by order_number
order by quantity desc
limit 10
SELECT QUANTITY, COUNT(QUANTITY) AS TOTAL_Q
FROM MYTABLE
GROUP BY QUANTITY
ORDER BY TOTAL_Q DESC
this will give you number of quanity from most to least number....