SQL Conditional Logic for BigQuery

35 views Asked by At

I am creating a TVF on my BigQuery which will allow me to take in two date arguments. Based on the date logic, I want to run and return different SQLs. Pseudo code is below-

CREATE OR REPLACE TABLE FUNCTION `f_tvf`(from_dttm DATE, to_dttm DATE) AS (
{% if var_to_date==CURRENT_DATE() and var_from_date>=DATE_ADD(CURRENT_DATE(),INTERVAL -61 DAY) %}
  SELECT 
  *
  FROM SinkA
  UNION ALL
  SELECT 
  *
  FROM SinkB
{% elif var_to_date<=DATE_ADD(CURRENT_DATE(),INTERVAL -1 DAY) and var_from_date >=DATE_ADD(CURRENT_DATE(),INTERVAL -61 DAY) %}
  SELECT 
  *
  FROM SinkB
{% else %}
  SELECT * FROM SinkA 
{% endif %}

I know if-else is not supported in BQ but if anyone can support an alternative architecture, i am more than happy to incorporate that

0

There are 0 answers