Bigquery select distinct values

前端 未结 6 855
离开以前
离开以前 2021-01-11 10:03

How to select distinct values in Google Bigquery?

Query:

SELECT DISTINCT cc_info
FROM user
WHERE date = ?

Thanks!

相关标签:
6条回答
  • 2021-01-11 10:39
    SELECT cc_info
    FROM user
    WHERE date = ?
    GROUP BY cc_info
    
    0 讨论(0)
  • For all who have come to find the DISTINCT method in BigQuery, and who needs to use unique field feature for tables having large columns, using GROUP BY as mentioned by tning won't be possible.

    As of 2020, BigQuery has DISTINCT modifier. You need to wrap your query as:

    SELECT DISTINCT usr.cc_info
    FROM (
      SELECT *
      FROM user
      WHERE date = ?
    ) usr
    

    This could be very handy for people shifting from other SQL products.

    0 讨论(0)
  • 2021-01-11 10:41

    Try using group by

    SELECT cc_info
    FROM user
    WHERE date = ?
    group by cc_info
    
    0 讨论(0)
  • 2021-01-11 10:43
    SELECT COUNT(DISTINCT cc_info)
    FROM user
    WHERE date = ?
    

    is NOT the right query, because DISTINCT is a statistical approximation and is not guaranteed to be exact. See https://cloud.google.com/bigquery/docs/reference/legacy-sql#countdistinct

    So better approach is

    select EXACT_COUNT_DISTINCT(cc_info) from user where date = ?
    
    0 讨论(0)
  • 2021-01-11 10:49

    Simply use group by,

    SELECT cc_info
    FROM user
    WHERE date = ?
    GROUP BY cc_info
    

    If you want to COUNT over DISTINCT values you can use,

    SELECT COUNT(DISTINCT cc_info)
    FROM user
    WHERE date = ?
    
    0 讨论(0)
  • 2021-01-11 11:00

    This is another way to achieve your goal (in case anyone else needs it) which works in the current BigQuery 2020.

    SELECT colname FROM table1
    UNION DISTINCT 
    SELECT colname FROM table2
    UNION DISTINCT
    .
    .
    .
    SELECT colname FROM tableN 
    

    My reference was this article.

    0 讨论(0)
提交回复
热议问题