Raymond Raymond / Code Snippets & Tips

BigQuery SQL - Retrieve DISTINCT Values

event 2021-03-13 visibility 4,304 comment 0 insights toc
more_vert
insights Stats
toc Table of contents

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"
  }
]
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts