Home > Back-end >  How to GROUP BY on multiple DISTINCT columns? ( Google Big Query )
How to GROUP BY on multiple DISTINCT columns? ( Google Big Query )

Time:02-05

  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

CodePudding user response:

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
  •  Tags:  
  • Related