[Solved] How to get non grouped-by columns in SQL statement (similar to in MySQL)


Below is for BigQuery Standard SQL and as simple as below

#standardSQL
SELECT ANY_VALUE(first_name) first_name
FROM `project.dataset.table`
GROUP BY age

As you can see you were missing just aggregation function – it can be any – MAX, MIN, etc. I’ve chosen ANY_VALUE as an example

You can test, play with above using some simplified dummy data like in example below

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'Thomas' first_name, 25 age UNION ALL
  SELECT 'Mike', 25 UNION ALL
  SELECT 'Thomas', 30 UNION ALL
  SELECT 'Mark', 40
)
SELECT ANY_VALUE(first_name) first_name
FROM `project.dataset.table`
GROUP BY age

with result

Row first_name   
1   Thomas   
2   Thomas   
3   Mark     

1

solved How to get non grouped-by columns in SQL statement (similar to in MySQL)