Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Mode in Postgres SQL

Tags:

sql

postgresql

I have a table like this -

cdr_pkey as serial, calldate as timestamp, src as text, duration as bigint

I need to find top 10 most frequent numbers in 'src' column, together with the number of occurrence in the table by using SQL query.

I tried by exporting to Excel and run Mode function but the records are around 7 million so it is not so efficient.

PS. I'm using PostreSQL 9.1

like image 762
Ye Myat Aung Avatar asked Nov 19 '25 23:11

Ye Myat Aung


1 Answers

select 
    src as text,
    count(*) as total
from t
group by 1
order by total desc
limit 10;
like image 153
Clodoaldo Neto Avatar answered Nov 22 '25 13:11

Clodoaldo Neto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!