markovuksanovic
markovuksanovic

Reputation: 15906

BigQuery and GROUP BY clause

I'm trying to figure out how Google BigQuery works in respect to aggregation and grouping. I read the documentation and it says for GROUP BY this:

The GROUP BY clause allows you to group rows that have the same values for a given field. You can then perform aggregate functions on each of the groups. Grouping occurs after any selection or aggregation in the SELECT clause.

So it says that after grouping I can perform aggregate functions (I assume that's functions like COUNT). But than the sentence later it says that grouping occurs after any selection or aggregation in the SELECT clause.

So if I have

SELECT f1, COUNT(f2)
  FROM ds.Table
  GROUP BY f1;

Which happens first, grouping or counting?

Upvotes: 0

Views: 3421

Answers (1)

Javier Ramirez
Javier Ramirez

Reputation: 4032

You will have the group and then the count. In your case you would get a single line for each f1 and then the count.

However, if you want to do something interesting, you could use window functions in which first you can group by some fields, and then you can execute functions against the resulting rows, which is quite handy.

Take a look at the window functions section of the bigquery online docs for a few examples on this.

Upvotes: 1

Related Questions