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
Related
I'm a bit stuck cause am not sure if I am looking the wrong way casue I can't find anything related to what I am looking for. So I want to give an example for what I am trying to do. NOTE: not real scenario.
I have a count on a field:
SELECT count(user_id)
FROM usersgroups
group by user_id
Now I want to make a calc with the max value from the count. Something like:
SELECT count(user_id) as count,
SUM(max(count(user_id)) / count(user_id)) as blub
FROM usergroups
group by user_id
But I get error invalid use of group function cause I think I cant use the COUNT() function inside the SUM function?
So is there any other way to make that calculation.
P.S. the calc is for calculating the percentage of the max value to the record/current value.
UPDATE
So what I expect is a percentage like
count | percentage
5 | 1
4 | 0.8
3 | 0.6
2 | 0.3
1 | 0.2
5/5 = 1
4/5 = 0.8
3/5 = 0.6
2/5 = 0.4
1/5 = 0.2
In a separate Derived Table, you can fetch the maximum count value out of all the counts. I have simply used ORDER BY COUNT(user_id) DESC LIMIT 1 to fetch the same.
CROSS JOIN this result-set with the usergroups table, so that every row in the usergroups table has access to the maximum count value.
Now, you can simply use the GROUP BY and appropriate aggregation to determine the "percent".
Note that, for GROUP BY to be valid, SELECT clause must contain either aggregated columns/expressions only, or the columns specified in the GROUP BY clause. That is why MAX() is used over the maximum count value. Since that value is only a scalar, so MAX() will return the same value only.
Try the following:
SELECT
ug.user_id,
COUNT(ug.user_id) AS user_count,
COUNT(ug.user_id) / MAX(mcnt.count) AS percent
FROM
usergroups AS ug
CROSS JOIN
(
SELECT COUNT(user_id) AS count
FROM usersgroups
GROUP BY user_id
ORDER BY COUNT(user_id) DESC LIMIT 1
) AS mcnt
GROUP BY ug.user_id
ORDER BY user_count DESC
Count available user_id as subquery, then divide user_id values by the sum of count.
select user_id/ sum(count) as percentage
from(
select count(user_id) as count , user_id
from usergroups
where user_id != 0
group by user_id
) as A
group by count, user_id
I did a sql query as following:
SELECT ip ,count(*) AS count FROM Abfragen WHERE sid = 1 GROUP BY ip ORDER BY count DESC limit 10
The result is:
ip count
52.28.9.253 35046
213.128.143.1 3860
5.10.190.222 106
52.58.240.24 58
52.58.99.201 42
81.184.0.178 30
217.159.201.54 6
but i want also the total record count of the result, for example here 7.
Can someone tell me how to get it? Thanks.
You can simply do it like this
Update:
Select Count(*)
From (Select `ip`,count(*) as count
FROM (`Abfragen`
where `sid` = 1
Group BY `ip`
ORDER BY count DESC limit 10) AS IP_COUNT
i am having two tables ss_test and ss_ceastore_config ss_test has field called store_id which maps to ss_ceastore_config id.
my ss_test contains servers entry which tells that which store it is using
so i am trying to find which store is used min times and its id.
i have below query written.
select id,
min(server_counts) as server_counts,
isalive
from (select ss_ceastore_config.id ,
count(server_id)as server_counts,
ss_ceastore_config.isalive
from ss_test
right join ss_ceastore_config
on ss_test.store_id = ss_ceastore_config.id
group by store_id
order by id) join_1
my inner query gives me proper result as below
id Ascending server_counts isalive
1 5 1
2 0 1
so i want to select below record from inner query output using min function
id Ascending server_counts isalive
2 0 1
but it gives unexpected result with my outer query as below
id server_counts isalive
1 0 1
why this so? why it is giving id 1 for server_counts 0?
how to fix this query?
select id,
server_counts,
isalive
from (select ss_ceastore_config.id ,
count(server_id)as server_counts,
ss_ceastore_config.isalive
from ss_test
right join ss_ceastore_config
on ss_test.store_id = ss_ceastore_config.id
group by store_id
order by id) join_1
order by server_counts asc
limit 1;
Your original query was not working because you were doing an aggregate SELECT using the MIN() function along with non-aggregate columns such as id and isalive. I believe that MySQL makes no guarantee about which id value it will return along with the minimum for that column.
My strategy is to return all rows in ascending order by server_counts, and then to SELECT only the first row (which is the minimum).
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....
im stuck here and i don't know how to fix it.
I have a db table which has users ID, user grade and date when someone has voted for that user (3 fields).
Im trying to read the user that has the highest average grade for todays date, limited to one.
But the problem is that i want to read only users that have 5 or more votes.
My query looks like this, but im getting an error:
SELECT
idusers,
AVG(votes) AS Grade
FROM rank
WHERE (data = '{$dbDate}')
AND ((SELECT count(ID) + 1 FROM rank) AS tmpcount WHERE tmpcount>4)
GROUP BY idusers
ORDER BY Grade DESC
LIMIT 1
Without the tmpcount>4 clause this query is working ok, but I need to count the Id's.
You have to use HAVING to filter the result set on aggregated values such as COUNT (SUM, MIN, MAX, AVG, …):
SELECT idusers, AVG(votes) AS Grade
FROM rank
WHERE (data = '{$dbDate}')
GROUP BY idusers
HAVING COUNT(*) > 4
ORDER BY Grade DESC
LIMIT 1