BigQuery SQL - Retrieve DISTINCT Values

Raymond Tang Raymond Tang 0 4725 3.01 index 3/13/2021

In earlier version of BigQuery SQL, DISTINCT clause is not supported. To retrieve distinct values, we had to use GROUP BY or Windowing function to deduplicate. In 2020, BigQuery already support DISTINCT clause thus we can directly use it in SELECT statement.

SELECT DISTINCT

WITH
  SRC AS (
  SELECT
    'A' AS Col1,
    1 AS Col2
  UNION ALL
  SELECT
    'A' AS Col1,
    1 AS Col2 )
SELECT
   DISTINCT *
FROM
  SRC;

Output (in JSON format):

[
  {
    "Col1": "A",
    "Col2": "1"
  }
]
bigquery gcp sql

Join the Discussion

View or add your thoughts below

Comments