How to find the minimum values of a query's column - mysql

how do you find the minimum value of a column of a query?
My table X's data looks like this:
(id, something, userId, something) values
('R001','something','U0006','something'),
('R002','something','U0014','something'),
('R001','something','U0006','something'),
('R002','something','U0015','something'),
('R003','something','U0003','something'),
('R001','something','U0014','something'),
('R001','something','U0002','something');
My query, looks like this:
SELECT DISTINCT userId, COUNT( id ) AS count
FROM X
GROUP BY userId
ORDER BY count DESC
and this query returns:
userId count
U0006 2
U0014 2
U0002 1
U0003 1
U0015 1
How do I get the minimum values of count in the query, bearing in mind that there are multiple minimum values?
The return I want from the query would look like this:
userId
U0002
U0003
U0015
Thanks, in advance :)

You can do this using a having clause:
SELECT userId, COUNT( id ) AS count
FROM X
GROUP BY userId
HAVING COUNT(id) = (SELECT MIN(cnt)
FROM (SELECT COUNT(id) as cnt
FROM X
GROUP BY userId
) xx
);
Using select distinct with group by is almost always redundant.

Related

Mysql query to find all in row when value in certain column appears

In my database table, I have a value denoted userID. The value user ID may appear multiple times. I am trying to write a query that will either return all data in the row of that table if the value in userID appears 3 or more times. Alternatively, a query that return a list of all userIDs that appear 3 or more times.
Here is what I have now:
SELECT
*
FROM
myTable
WHERE
userID IN (SELECT
userID
FROM
myTable
GROUP BY userID
HAVING COUNT(*) > 2)
GROUP BY userID
This query kinda works, but some of the rows returned only have a single or double occurrence.
Any ideas on how I can modify this query so that it works?
You made things harder tnen they are, it's all simple:
SELECT
userID
FROM
myTable
GROUP BY userID
HAVING COUNT(userID) > 2)
This is if you only need ID's, else you should use this in place of SELECT statement in your WHERE part
Add the rest of your columns in the GROUP BY userID for all of the rows and youre super close with the list of userID's. This'll give you the UserID's:
SELECT userID FROM your_table
GROUP BY userID
HAVING COUNT(*) >= 3
and this will be for all:
SELECT
*
FROM
your_table
WHERE
userID IN (SELECT
userID
FROM
your_table
GROUP BY userID
HAVING COUNT(*) >= 3)
GROUP BY UserID, column_name1, column_name2

sql return most prevalent column value

I'm a beginner at SQL, how do I get a query which returns the most prevalent column value? Probably there is an answer somewhere but I don't know how to google it.
For example in the user_id column the query should return the value 1 because this is the most prevalent number.
One approach is to do a GROUP BY aggregation and then apply a LIMIT trick:
SELECT user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1;
If you want something more complex, then you would be getting into the realm of rank functionality. MySQL (at least as of the current release) does not support built-in rank support, so it can be tricky to perform such queries.
SELECT top 1 user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
ORDER BY COUNT(*) DESC
Have a common table expression that counts each user_id. Select user_id where the count is the max count. Will return both user_id's in case of a tie.
with cte as
(
SELECT user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
)
select user_id
from cte
where cnt = (select max(cnt) from cte)

mysql COUNT results with GROUP BY

I have a table with with 2 unique linked table ids.
I get the results I want with GROUP BY but when I count I only get the number of each group.
When I do:
SELECT COUNT(id) FROM my_table GROUP BY first_linked_table_id, second_linked_table_id
I get as results 1, 2, 3, 1 but I want 4 as a result.
I tried DISTINCT but I think that only works with one column
Your requirement is to get count of number of groups. So we need two operations-
Group(inner query)
Count(outer query)
Following query will do precisely that:
SELECT COUNT(*)
FROM
(
SELECT COUNT(id)
FROM my_table
GROUP BY first_linked_table_id,
second_linked_table_id
) t
If you want to count the rows, I think you're going to need a subquery. Something like this:
SELECT COUNT(*) FROM (
SELECT COUNT(id) FROM my_table GROUP BY first_linked_table_id, second_linked_table_id
);

Maximum Count of Distinct Values in SQL

please forgive me if this has been answered, but could not find it using the search tool or a basic google query.
I am trying to return a value that indicates the maximum number of rows any distinct value in a column in SQL.
For example, I'd like to use something like
SELECT MAX(COUNT(DISTINCT person_id) AS MAX_NUM_PERS_ROW
FROM mytable
and if the person with most rows in the table had 5 rows, the value returned would be 5...
Any and all help is appreciated!
You can do this with nested aggregation:
select max(cnt)
from (select person_id, count(*) as cnt
from mytable
group by person_id
) p;
If you actually want the person, you can also do:
select person_id, count(*) as cnt
from mytable
group by person_id
order by count(*) desc
limit 1;

MYSQL: Count the number of times a specific integer appears in a column then making a count of how many of these are present?

I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;