Search code examples
google-bigquerycoalesce

BigQuery Standard Get first not null value when grouping


I have a table like this:

CUSTOMERS_ID  DATE_SALES  DIMENSION
MARIO1        20200201    NULL
MARIO1        20200113    Spain
MARIO2        20200131    NULL
MARIO3        20200101    France
MARIO3        20191231    Spain

and I need to order by CUSTOMERS_ID and DATE_SALES DESC fields. Then I want to group by CUSTOMERS_ID field and get first not null value of DIMENSION field. The output table would be:

CUSTOMERS_ID  DIMENSION
MARIO1        Spain
MARIO2        NULL
MARIO3        France

Any ideas? I have tried COALESCE functions, FIRST_VALUE, and I have not the results I had expected.

Thanks in advance!


Solution

  • Below is for BigQuery Standard SQL

    #standardSQL
    SELECT AS VALUE ARRAY_AGG(t ORDER BY IF(DIMENSION IS NULL, NULL, DATE_SALES) DESC LIMIT 1)[OFFSET(0)]
    FROM `project.dataset.table` t
    GROUP BY CUSTOMERS_ID   
    

    if to apply to sample data from your question - result is

    Row CUSTOMERS_ID    DATE_SALES  DIMENSION    
    1   MARIO1          20200113    Spain    
    2   MARIO2          20200131    null     
    3   MARIO3          20200101    France