Search code examples
google-bigquerybigquery-udf

GoogleBigQuery How to exclude null values from a specific column in a union query?


I would like to filter out null values from a specific column in this case "Account" on a union query how can i do this?

The query that I am working on is this:

SELECT Account, Campaign_name, Ad_group_name, Date, Keyword, Impressions, Clicks, Cost__GBP_, Conversions FROM table1 UNION ALL

SELECT Account, Campaign_name, Ad_group_name, Date, Keyword, Impressions, Clicks, Cost__GBP_, Conversions FROM table2

Someone can help me in this? Thanks


Solution

  • Each query is evaluated separately in a union query. So you should add WHERE filters to each of them.

    SELECT Account, Campaign_name, Ad_group_name, Date, Keyword, Impressions, Clicks, Cost__GBP_, Conversions 
    FROM table1 
    WHERE Account is not NULL
    
    UNION ALL
    
    SELECT Account, Campaign_name, Ad_group_name, Date, Keyword, Impressions, Clicks, Cost__GBP_, Conversions 
    FROM table2
    WHERE Account is not NULL