Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery SPLIT() and grouping by result

Using SPLIT() & NTH(), I'm splitting a string value, and taking the 2nd substring as the result. I then want to group on that result. However, when I use SPLIT() in conjunction with a GROUP BY, it keeps giving the error:

Error: (L1:55): Cannot group by an aggregate

The result is a string, so why is it not possible to group on it?

For example, this works and returns the correct string:

SELECT NTH(2,SPLIT('FIRST-SECOND','-')) as second_part FROM [FOO.bar] limit 10

enter image description here

But then grouping on the result does not work:

SELECT NTH(2,SPLIT('FIRST-SECOND','-')) as second_part FROM [FOO.bar] GROUP BY second_part limit 10

enter image description here

like image 888
Graham Polley Avatar asked May 15 '15 00:05

Graham Polley


People also ask

How do you split values in BigQuery?

Working with Strings in Google BigQuery It divides value using the delimiter argument. For STRING , the default delimiter is a comma. For BYTES , you must specify a delimiter. Splitting with an empty delimiter generates an array of UTF-8 characters for STRING values and an array of BYTES for BYTES values.

How do I concatenate two columns in BigQuery?

The BigQuery CONCAT function allows you to combine (concatenate) one more values into a single result. Alternatively, you can use the concatenation operator || to achieve the same output.

What is coalesce in BigQuery?

COALESCE(expr[, ... ]) Description. Returns the value of the first non-null expression. The remaining expressions are not evaluated.

How do you concatenate in a BigQuery?

The Concatenation can be performed in BigQuery using the BigQuery CONCAT function or using the concatenation operator (“ || “). Both methods combine two or more BYTE into a single entity. BigQuery CONCAT String can combine STRING entities.


1 Answers

My best guess would be that you can get an equivalent result by using a subquery. Something like :

SELECT * FROM (Select NTH(2,SPLIT('FIRST-SECOND','-')) as second_part FROM [FOO.bar] limit 10) GROUP BY second_part 

The system returns Nth in an aggregate internally I guess

like image 96
Patrice Avatar answered Sep 28 '22 12:09

Patrice