Ridhima
Ridhima

Reputation: 13

For a list of items return the top 3 values based on their count

For a given list of items in a column,I want it to give me top 3 values based on their count.

I would expect Cat, Dog and Donkey in another column.

enter image description here

Upvotes: 1

Views: 358

Answers (2)

marikamitsos
marikamitsos

Reputation: 10573

Please use the following formula

=QUERY(QUERY(A2:A11, 
     "select A, count(A) where A is not null group by A limit 3 label count(A) ''"), 
                 "select Col1")

(You can adjust ranges to your needs)

enter image description here

Functions used:

Upvotes: 1

lolitsjef
lolitsjef

Reputation: 81

IF you are using SQL you can get them using COUNT and then the fieldname. If you are using a language not built around storing data you could run a for loop that updates a count variable for each element in the list. ie:

    for (int i =0; i++; i < list.length) {
    if( list[i] == cat)
    catCounter++;
    if (list[i]) = dog)
    dogCounter++;
    .
    .
    .
    }

Upvotes: 0

Related Questions