Get most frequent value with SQL query -


i'm trying write sql query find value occurs frequently.

so far, have this:

select genre, count(*) frequency  booksread  group genre 

this gives me output this:

anthropological         1  biography               7  crime                   4  essay                   2  

i want returned result 7. i've tried using top 1 java compiler doesn't seem it.

the ansi sql syntax be:

select genre, count(*) frequency booksread group genre order count(*) desc fetch first 1 row only; 

not databases support syntax. many support limit:

select genre, count(*) frequency booksread group genre order count(*) desc limit 1; 

however, exact syntax depends on database using.

you can use ansi standard window functions:

select * (select genre, count(*) frequency,              row_number() on (order count(*) desc) seqnum       booksread       group genre      ) g seqnum = 1; 

if want ties use rank() instead of row_number().


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -