Skip to content
Advertisement

BigQuery: Best way to ORDER BY an arbitrary GROUP BY expression?

In the below query, I want to group all orders by the concatenation of firstname and lastname, and order the result set by that concatenation. However, BigQuery returns the following error for the query:

SELECT 
    COUNT(o.ORDERID)
FROM EMPLOYEES e
INNER JOIN ORDERS o ON e.EMPLOYEEID = o.EMPLOYEEID
GROUP BY CONCAT(e.FIRSTNAME, ' ', e.LASTNAME)
ORDER BY CONCAT(e.FIRSTNAME, ' ', e.LASTNAME)

ORDER BY clause expression references column [column_name] which is neither grouped nor aggregated

I can fix this by wrapping the ORDER BY clause in ANY_VALUE:

SELECT 
    COUNT(o.ORDERID)
FROM EMPLOYEES e
INNER JOIN ORDERS o ON e.EMPLOYEEID = o.EMPLOYEEID
GROUP BY CONCAT(e.FIRSTNAME, ' ', e.LASTNAME)
ORDER BY ANY_VALUE(CONCAT(e.FIRSTNAME, ' ', e.LASTNAME))

Is this best practice? Is there a more canonical way to do this for arbitrary GROUP BY expressions? Other DBMS, including MySQL, SQLServer, Postgres, etc. handle the first query without error.

Advertisement

Answer

You can just use numbers or references. Also, you can have the GROUP BY key in the SELECT. I would suggest:

SELECT CONCAT(e.FIRSTNAME, ' ', e.LASTNAME) as fullname
       COUNT(o.ORDERID)
FROM EMPLOYEES e INNER JOIN
     ORDERS o
     ON e.EMPLOYEEID = o.EMPLOYEEID
GROUP BY fullname
ORDER BY fullname;

You can also write the last two lines as:

GROUP BY 1
ORDER BY 1
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement