Search code examples
sqldistinct

How to GROUP BY on multiple DISTINCT columns? ( Google Big Query )


  1. The following code GROUPs by date, and counts DISTINCT "a" values.
    SELECT Date,
           COUNT(DISTINCT(a)),
    FROM process
    GROUP BY date
  1. The following code shows the number of DISTINCT values in "a" and "b" columns:

    SELECT COUNT(*)
    FROM (SELECT DISTINCT a, b
          FROM process)

TRIED: If I write this:

SELECT Date,
       COUNT(DISTINCT(a, b)),
FROM process
GROUP BY date

I get

ERROR: Aggregate functions with DISTINCT cannot be used with arguments of type STRUCT

QEUSTION: I need to count distinct values from "a" and "b" columns. How do I combine 1 and 2 codes?

Columns and Values explained: date: TIMESTAMP (DATE WAS EXTRACTED) a = INT b = STRING

Desired outcome table: date (grouped) : 13.09.2002 distinct(count): 232


Solution

  • if you need to get distinct a, b combinations then this one could help:

    SELECT
           Date,
           COUNT(DISTINCT(CONCAT(a,'_',b))) distinct_a_b
      FROM process
     GROUP BY date