The reason why comparison and conditional aggregation fail in BigQuery

The reason why comparison and conditional aggregation fail in BigQuery

Why did comparison and conditional aggregation fail in BigQuery ?

BigQuery(BQ) is very useful for data analysis or processing.
It is good at handling huge data. It returns summary result in short time.

When we handle big data, sometimes we want to extract or summarize data that meets specific conditions.
Usually we use comparison operator like below.

WHERE COLUMN1 = "A"
SUM(IF(COLUMN1 = "A",1,0)

But it fails in specific case.

Why did it fail ?

So today I introduce about "Why comparison and conditional aggregation fails in BigQuery".

目次

Prepare data

First, prepare data.

Import this csv file as null_sample table.

This data has null cells.

col1,col2,col3
a,b,c
,b,c
,,c
a,b,
a,,

Then you can see table like below.

Table

Rowcol1col2col3
1nullbc
2nullnullc
3abc
4abnull
5anullnull

Comparison and conditional aggregation

There is a good example of comparison and conditional aggregation.

We use operator =.

SELECT * FROM test.null_sample
WHERE col1 = "a"

Result

Rowcol1col2col3
1abc
2abnull
3anullnull

In order to get conditional sum, we can use SUM and IF function.

SELECT
SUM(IF(col1="a",1,0)) as sum_col1_a
FROM test.null_sample

Result

Rowsum_col1_a
13

Example of failure case

Then there is an example of failure.

When we use != , its comparison does not work.

SELECT * FROM test.null_sample
WHERE col1 != "a"

Result

No result.

Even in case of conditional aggregation, it fails like below.

SELECT
SUM(IF(col1="a",1,0)) as sum_col1_a,
SUM(IF(col1!="a",1,0)) as sum_col1_not_a,
SUM(IF(col2!="a",1,0)) as sum_col2_not_a,
SUM(IF(col3!="a",1,0)) as sum_col3_not_a
FROM test.null_sample

Result

Rowsum_col1_asum_col1_not_asum_col2_not_asum_col3_not_a
13033

Sample table has 5 records.

In the table, about non- a count, it should be 2 in col1.

And it should be 5 in col2 or col3.

Why did it fail to aggregate ?

The reason why comparison and conditional aggregation fail in BigQuery

The reason why comparison and conditional aggregation fails in BigQuery is null.

null is special that does not return true to both = nor !=.

So if you want to compare data, you should replace null with other value by IFNULL or COALESCE.

SELECT * FROM test.null_sample
WHERE IFNULL(col1,"") != "a"

Result

Rowcol1col2col3
1nullbc
2nullnullc

For conditional aggregation, we should use IFNULL or COALESCE.

SELECT
SUM(IF(IFNULL(col1,"")="a",1,0)) as sum_col1_a,
SUM(IF(IFNULL(col1,"")!="a",1,0)) as sum_col1_not_a,
SUM(IF(IFNULL(col2,"")!="a",1,0)) as sum_col2_not_a,
SUM(IF(IFNULL(col3,"")!="a",1,0)) as sum_col3_not_a
FROM test.null_sample

Result

Rowsum_col1_asum_col1_not_asum_col2_not_asum_col3_not_a
13255

In this case we got correct non-a records and non-a count.

Conclusion

Today I explained about "Why comparison and conditional aggregation fails in BigQuery".

The reason why comparison and conditional aggregation fails in BigQuery is null.

It fails when we try to compare null directly.

Import point is below.

Replace null by IFNULL or COALESCE

Handling null is complicated.

Conditional expressions | BigQuery | Google Cloud

The reason why comparison and conditional aggregation fail in BigQuery

この記事が気に入ったら
いいね または フォローしてね!

If you like this article, please share !
  • URLをコピーしました!
  • URLをコピーしました!

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

目次