Site icon Bitcoins Tips

Conditional column in BigQuery that depends on its own value in previous rows and another existing column

I have an SQL table in BigQuery that has:

What I need is to create a conditional boolean column, let’s call it “Alert“, that has a true value when:

Identifying the very first alert for each partition is simple using a window function. The challenge is with all further alerts, because Alert should depend on its own earlier values.

Expected output should be like this (Comment column is added here only for clarity):

Date Trigger Alert Comment
2021-12-01 FALSE 0 Trigger is false
2021-12-02 FALSE 0 Trigger is false
2021-12-03 TRUE 1 No earlier alerts in the last three days
2021-12-04 TRUE 0 Alert during last three days
2021-12-05 TRUE 0 Alert during last three days
2021-12-06 TRUE 0 Alert during last three days
2021-12-07 TRUE 1 No earlier alerts in the last three days
2021-12-08 FALSE 0 Trigger is false
2021-12-09 FALSE 0 Trigger is false
2021-12-10 TRUE 0 Alert during last three days

I’d appreciate any tips, thanks in advance!

Exit mobile version