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....
Related
Im trying to get a maximum value after im preforming a groupby clause.
select count(*) as count, store_id from sakila.customer
group by store_id
my output for this code is:
count
store_id
326
1
273
2
how can i get a max value from the count column? i tried several things and nothing seems to work.
Just order your results and limit them to 1:
select count(*) as count, store_id from sakila.customer
group by store_id
order by count desc
limit 1
I want to fetch last row of the table. For this i have used to below query but it returns me 99 where my table contains approx 123 and last pro_id = 123. Right result should be 123.
please suggest me for right way :
SELECT * FROM product ORDER BY pro_id DESC LIMIT 1
That's most likely because your pro_id column is defined as text.
You can tell sql to order by the numeric value in that column with
SELECT * FROM product ORDER BY CONVERT(pro_id, UNSIGNED INTEGER) DESC LIMIT 1
I think the problem is that your pro_id is character and it will be ordered by alphabetical order. You could try to convert it to number first.
SELECT * FROM product ORDER BY CAST(pro_id AS UNSIGNED) DESC LIMIT 1
This query will display a true desc result:
select pro_id from
(SELECT cast(replace(pro_id,' ','') as UNSIGNED) as pro_id from product) as a
ORDER BY pro_id desc limit 1
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;
I am trying to group multiple rows and order it by the total values but im struggling to figure out whats going wrong.
Name Total
======= =======
ASOS 222
Tesco 11
ASOS 11111
Tesco 123
The table should look like this
Name Total
======= =======
ASOS 11111
ASOS 222
Tesco 123
Tesco 11
I thought this query would work
select * from tablename order by name asc, total asc
But that shows a result in the wrong order.
Any help would be appreciated.
Try this
select * from tablename order by total desc
Selecting two things to ORDER BY doesn't work too well if you're not familiar with ORDER BY syntax. from your description, it looks like you just want the highest total at the top. This query will order the results by total descending (highest first)
if you want the names to be ascending (lowest first) at the same time, try
select * from tablename order by name asc, total desc
First order by the max total for each name, descending, the order by total descending:
select *
from tablename t1
order by (select max(total) from tablename t2
where t1.name = t2.name) desc,
total desc
Try this:
SELECT *
FROM tablename
ORDER BY
Name,
char_length(CAST(Total As varchar(max))) DESC,
Total DESC
I have a table with checkin records like this:
userID checkin_date
1 '2014-01-28 08:00:00'
1 '2014-01-27 09:10:00'
1 '2014-01-26 12:24:00'
2 '2014-01-26 08:17:00'
3 '2014-01-26 09:33:00'
2 '2014-01-28 10:28:00'
.. .........
and i want with a single request sort the ten users who checkin the most since a specific date (order by nb visite DESC) (that easy) but for each one i also want to know the date of their last checkin.
i do something like this:
SELECT
userID,
count(*) as nbVisit,
checkin_date
FROM(
SELECT
userID,
checkin_date
FROM checkin_table
WHERE checkin_date > '2014-01-25'
ORDER BY checkin_date DESC )as sub
GROUP BY userID
ORDER BY nbVisit DESC
LIMIT 10
is it the best way to do it ? will it work in any time ? is it efficient with lots of data ?
SQLFIDDLE
You don't need a subquery for this, just use max() along with count(*):
SELECT userID, max(checkin_date), count(*) as nbVisit,
FROM checkin_table
WHERE checkin_date > '2014-01-25'
GROUP BY userId
ORDER BY nbVisit desc
LIMIT 10 ;