Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bigquery ORDER BY (count )

I want to run following query into Bigquery

SELECT table1.field1, COUNT(table1.fiels2)
FROM table1
GROUP BY table1.fiels1
ORDER BY COUNT(table1.fiels2)
DESC limit 10;

I am getting error Error: (L1:282): expression COUNT([metamx_magnetic_share.adops001.cookie], DESC) in ORDER BY is invalid

But same query I am successfully ran on Vertica.

Any kind of help/suggestion would be appreciated.

Thanks

like image 379
roy Avatar asked Jun 02 '14 21:06

roy


People also ask

What is the use of count BigQuery?

The use of the COUNT function in Bigquery is to return a single value from the number of rows in the input. The DISTINCT clause with COUNT is used only to eliminate any duplicate row in the table.

How do you offset in BigQuery?

OFFSET means that the numbering starts at zero, ORDINAL means that the numbering starts at one. A given array can be interpreted as either 0-based or 1-based. When accessing an array element, you must preface the array position with OFFSET or ORDINAL , respectively; there is no default behavior.

How do I sort an array in BigQuery?

Results from your query can be sorted by using the order_by argument. The argument can be used to sort nested objects too. The sort order (ascending vs. descending) is set by specifying the asc or desc enum value for the column name in the order_by input object, e.g. {name: desc} .


1 Answers

You can sort by an alias, but not a function, so try:

SELECT table1.field1, COUNT(table1.fiels2) as cnt
FROM table1
GROUP BY table1.fiels1
ORDER BY cnt
DESC limit 10;

The documentation is here.

like image 197
Gordon Linoff Avatar answered Sep 20 '22 04:09

Gordon Linoff