Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big Query Order By Grouped Field

I have a query that groups by date which works fine.

SELECT EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern')) date, SUM(users) total_users FROM `mydataset.mytable` 
GROUP BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'))

but when I try to order by date:

SELECT EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern')) date, SUM(users) total_users FROM `mydataset.mytable` 
GROUP BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'))
ORDER BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'));

I get the following error:

SELECT list expression references column timestamp which is neither grouped nor aggregated at [1:35]

The timestamp column is clearly part of the group by and even stranger still is that it works without the ORDER BY clause... What's going on here?

like image 803
RSHAP Avatar asked Jun 04 '18 13:06

RSHAP


People also ask

Does group by column order matter?

No, the order doesn't matter for the GROUP BY clause. MySQL and SQLite are the only databases I'm aware of that allow you to select columns which are omitted from the group by (non-standard, not portable) but the order doesn't matter there either.

How do you sort in BigQuery?

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.

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.

What is Array_agg in BigQuery?

The ARRAY_AGG BigQuery Functions returns an ARRAY of expression values. It is basically the opposite of UNNEST (used to flatten an array into its components to make it usable for analysis and database operations).


2 Answers

#standardSQL
SELECT 
  EXTRACT(DATE FROM DATETIME(timestamp, 'US/Eastern')) date, 
  SUM(users) total_users 
FROM `mydataset.mytable` 
GROUP BY 1
ORDER BY 1 
like image 187
Mikhail Berlyant Avatar answered Sep 23 '22 07:09

Mikhail Berlyant


You can try subselect:

#standardSQL
SELECT
  date,
  total_users
FROM (
  SELECT
    EXTRACT(date FROM DATETIME(timestamp,'US/Eastern')) date,
    SUM(users) total_users
  FROM
   `mydataset.mytable`
  GROUP BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'))
  )
ORDER BY
   date
like image 39
Vasily Bronsky Avatar answered Sep 20 '22 07:09

Vasily Bronsky