Search code examples
sortinggoogle-sheetsgroup-bycountgoogle-query-language

List non-unique entries and their counts sorted in descending order


If I have a list of names in a sheet for example:

First Name|Last Name|Something else|
Maria|Miller|...|
John|Doe|...|
Maria|Smith|...|
Marc|Meier|...|
Marc|Park|...|
Maria|Muster|...|
Selene|Mills|...|
Adam|Broker|...|

And then I want a second sheet which then shows the list of non-unique first names and their count, and the list being in descending order. So in this example that would be:

First Name|Count
Maria|3
Marc|2

What I found was this example https://infoinspired.com/google-docs/spreadsheet/sort-by-number-of-occurrences-in-google-sheets/ which sorts of partitions the sheet entries by occurrence.

So as of now I have

=UNIQUE(sort(
     Names!C3:Names!C12000;
     if(len(Names!C3:Names!C12000);countif(Names!C3:Names!C12000;Names!C3:Names!C12000););
     0;
     2;
     1
))

In the first column and

=IF(ISBLANK(A2);;COUNTIF(Names!C3:Names!C12000; A2))

In the second. This does the job somewhat (it still shows the names with count 1), but the second column needs a copying of each cell downwards for each new entry leftwards. Is there a way to tie this up directly in one line? While filtering out the unique occurrences at that. (Also the formulas are quite slow. The names sheet has about 11k entries so far. These formulas make the sheet crash at times atm. So I kind of want to sorts of comment out the formulas most of the time and only display them by commenting out the formulas. So the second column also just being one formula would be very helpful.)


Solution

  • use:

    =QUERY(SORT(QUERY(A2:A, "select A,count(A) group by A"), 2, ), "where Col2>1", )
    

    enter image description here