Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count unique in group by - Google Query

I have this table:

A B
1 4
2 5
3 6
1 4
2 4
3 4

How could I output:

A count
1 1
2 2
3 2

i.e. count records with distinct value.

I have read that in mysql I could do:

select A, count(distinct B) from table group by A

How can I transform this to Google Spreadsheet Query language?

like image 603
sites Avatar asked Mar 15 '14 17:03

sites


People also ask

How do you count unique values in Google?

Identify the cell you want to display the count of unique values and enter the formula. The formula to count unique values is =COUNTUNIQUE(A1:A10).

How do you count unique values in BigQuery?

How COUNT(DISTINCT [field]) Works in Google BigQuery. Typically in SQL database engines, the use of COUNT(DISTINCT [field]) within a query is used to count the exact number of DISTINCT items within the specified field.


2 Answers

One way to do it is

1- In Column C1, enter a formula =Unique(A1:A6). This will give you the unique items in Column A

enter image description here

2- Then in Column D1, enter =COUNTUNIQUE(FILTER($B$1:$B$6,$A$1:$A$6=C1)) and drag it down

enter image description here

like image 155
Pankaj Jaju Avatar answered Sep 27 '22 20:09

Pankaj Jaju


=QUERY(unique(A1:B7), "select Col1, count(Col2) group by Col1")

generally, the first part makes a "table" with only unique values and the select counts and groups.

like image 36
marc meyer Avatar answered Sep 27 '22 19:09

marc meyer