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?
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 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.
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
2- Then in Column D1, enter =COUNTUNIQUE(FILTER($B$1:$B$6,$A$1:$A$6=C1))
and drag it down
=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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With