Select row with maximum value from table - mysql

I have a table with non-unique column auth_id. I need to select the auth_id value with maximum number of entries.
SELECT auth_id, cnt
FROM (SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id) articles_num
WHERE cnt = (SELECT MAX(articles_num.cnt))
Here's the data example:
auth_id article_id
1 2
1 1
1 3
2 2
3 1
3 2
And the output:
auth_id cnt
1 3
But SQL doesn't see the alias table articles_num.
How do I make this WHERE clause with this alias?

Using a limit clause would be much simpler - you simply order a query according to some field, and then just take the first row:
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY 2 DESC
LIMIT 1

Order your data in descending order in your inner query then just take the first one:
SELECT auth_id, cnt
FROM (
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY cnt DESC
)
LIMIT 1

If I understand correctly, you actually want to get one row of the max of the count:
SELECT auth_id, count(auth_id) as cnt
FROM articles_authors
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1
If more than one auth_id have same max count, we need to update this SQL

Try this.......
select auth_id, count(auth_id) as cnt
from articles_authors
group by auth_id
order by cnt DESC
limit 1;
Let me know if it resolves your issue

Related

random order for grouped and limited query

I have a query like
SELECT name,count(*) cnt group by b order by cnt desc limit 10;
So I will have a list of items like
Name Count
test 6
test2 4
test6 1
test23 1
test4 1
The problem is that there are 1000s results with cnt=1 and I would like to show them randomly, otherwise I'd always show more or less the same for every query. What I would like is a random order on the rows with same cnt value. Is it doable?
You can use RAND() in order by after your count
SELECT name,count(*) cnt from t
group by b order by cnt desc ,RAND()
limit 10;
So first rows will be ordered by cnt in descending order then for the same value of count (cnt) rows will be ordered randomly
Sample demo with provided dataset

How to find user with MOST rows in table - mysql?

If you have a column called userID, what query would you use to find the userID with the greatest number of rows in that table?
Thank you
You can use COUNT, ORDER BY that count DESC and LIMIT the result to the top one:
SELECT user_id, COUNT(*)
FROM tableName
GROUP BY user_id
ORDER BY 2 DESC
LIMIT 1;
select user_id, sum(1) as counter
from TABLE
group by user_id
order by 2 desc
limit 1
This should be enough to get only one user, even if more than one user share the maximum amount of rows:
SELECT user_id FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
If you need to return all matching users:
SELECT user_id FROM table
GROUP BY user_id
HAVING COUNT(*) = (
SELECT COUNT(*) FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
)

How to select only the last value in a query in MySQL?

How can I select on MySQL the last value of this result:
This is a result from query:
SELECT * from transaction WHERE transaction_id = 2
I just need the last value 3 300 2
bank_id amount transaction_id
1 800 2
3 50 2
3 300 2
If bank_id is not unique and you want to pick the record of highest amount first, you can try this one:
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id DESC
, amount DESC
LIMIT 1
See this SQLFiddle
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id desc
LIMIT 1
Here is SQLFiddel Demo
This Demo, selects Last Entry in Transaction table with your filter of Transaction_ID = 2.
Below is the Query which you can try.
select *,#curRow := #curRow + 1 AS row_number
from Temp
Join (SELECT #curRow := 0) r
where Transaction_id = 2
order by row_number desc
limit 1
try out this..
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY transaction_id DESC
LIMIT 1

MYSQL DISTINCT and ORDER BY together

I have a table like the following
item_id position_number position_date
1 9 2013-06-29 15:12:58
2 7 2013-07-25 15:12:58
18 5 2013-07-08 12:07:00
13 9 2013-07-08 12:07:00
I want to get the items group by position_number and order by position_date DESC, so the query will return the following:
item_id position_number position_date
13 9 2013-07-08 12:07:00
2 7 2013-07-25 15:12:58
18 5 2013-07-08 12:07:00
I've been implementing some of the solutions that use DISTINCT and GROUP BY, but not get the desired result.
Does anyone have an idea about how to solved it?
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT position_number, MAX(position_date) position_date
FROM tableName
GROUP BY position_number
) b ON a.position_number = b.position_number AND
a.position_date = b.position_date
ORDER BY a.position_number DESC
SQLFiddle Demo
Given your example data, this query will return the specified resultset:
SELECT t.item_id
, t.position_number
, t.position_date
FROM ( SELECT MAX(n.item_id) AS max_item_id
FROM mytable n
GROUP BY position_number
) m
JOIN mytable t
ON t.item_id = m.max_item_id
ORDER BY t.position_number DESC
NOTE This is choosing a single item_id for each position_number. This is assuming that a given item_id will appear only once, and have a single position_number. (If an item_id can be associated with multiple postion_number, the query can be tweaked. This is using the MAX() function to choose the item_id with the largest value. (The only example of a row being excluded is item_id=1.)
try this
select * from your_table group by position_number order by position_date DESC
EDIT:
SELECT `item_id`, max(`position_number`) position_number , max(`position_date`) position_date FROM TABLENAME
GROUP BY POSITION_NUMBER
ORDER BY POSITION_DATE DESC
DEMO
Here is SQLFiddle
SELECT * FROM TABLE_NAME
GROUP BY POSITION_NUMBER
ORDER BY POSITION_DATE DESC
Try this code:
SELECT * FROM TABLE_NAME GROUP BY POSITION_NUMBER ORDER BY POSITION_DATE DESC
Use GROUP_BY and either MIN or MAX to decide which date in the group you want to use for sorting.
SELECT * FROM my_table GROUP BY position_number ORDER BY MAX(position_date) DESC;

Need Select top 10 people SQL

I need SQL query for MySQL to select top 10 people with most followers
my table
id | user_id | follow_id
1 3 6
2 3 7
3 4 6
4 5 6
5 7 3
6 9 7
From example user with id 6 have 3 time followed , 7->2 and 3->1, so TOP 10 will be
user with id 6,7,3 ...
SELECT `follow_id`, COUNT(1) AS `followers`
FROM `tbl`
GROUP BY `follow_id`
ORDER BY COUNT(1) DESC
LIMIT 10;
You want to use MySQL GROUP BY aggregation funciton
SELECT user_id, COUNT(follow_id) AS total_followers
FROM users
GROUP BY follow_id
ORDER BY total_followers LIMIT 10;
SELECT follow_id,count(id) AS cnt FROM table
GROUP BY follow_id ORDER BY cnt DESC LIMIT 10
You can use
SELECT user_id , COUNT(id) AS count FROM tbl GROUP BY follow_id ORDER BY count DESC LIMIT 10;
You need to group the results by the follow_id and then count how many results are in this group and sort this by the number of results per group in a descending order and then define you want to limit it to only 10 results which can be done by using LIMIT 0,10
The following query works perfectly in MySQL 5
SELECT follow_id, COUNT(follow_id) AS nr
FROM test.testtable
GROUP BY follow_id
ORDER BY nr DESC
LIMIT 0,10
Try some thing like this:
select follow_id
from myTable
group by follow_id
order by count(user_id)
Limit 10
SELECT follow_id,
COUNT(user_id) AS number_of_followers
FROM table
GROUP BY follow_id
ORDER BY number_of_followers DESC
LIMIT 10;