Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display the most common value sequence in MySQL?

Lets say I have a table like this.

A | B
-----
1 | 4
7 | 8
1 | 3
1 | 2
4 | 5
3 | 5
2 | 5
7 | 8

Even though 1 is the most common value in column A and 5 is the most common value in column B (which can be checked pretty simply with a COUNT()) the result I am looking for is the pair <7, 8> since that pair appears twice.

So how can I get the most common value sequence from a table, since I have n columns that I need to do this with.

like image 672
Ólafur Waage Avatar asked May 16 '11 17:05

Ólafur Waage


People also ask

How do I find the most common value in SQL?

How do you find the most repeated value in a column SQL? select cnt1. column_name from (select COUNT(*) as total, column_name from table_name group by column_name) cnt1, (select MAX(total) as maxtotal from (select COUNT(*) as total, column_name from table_name group by column_name)) cnt2 where cnt1.

How do you sequence in MySQL?

The simplest way for creating a sequence in MySQL is by defining the column as AUTO_INCREMENT during table creation, which should be a primary key column.

Which one is used to put the same value in all the rows?

The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has same values in different rows then it will arrange these rows in a group.


1 Answers

select A, B, count(*) as freq
from MyTable
group by A, B
order by count(*) desc
limit 1
like image 93
D'Arcy Rittich Avatar answered Sep 27 '22 23:09

D'Arcy Rittich