How to generate with scripting INTERVAL 1 ?

前端 未结 1 422
有刺的猬
有刺的猬 2021-01-21 18:19

We are trying to find a syntax to generate the DAY|WEEK|MONTH options from the 3rd param of date functions.

DECLARE var_date_option STRING DEFAULT \'DAY\';
sele         


        
相关标签:
1条回答
  • 2021-01-21 18:52

    Below is for BigQuery Standard SQL

    Those DAY|WEEK|MONTH are LITERALs and cannot be parametrized
    And, as you know - dynamic SQL is also not available yet

    So, unfortunately below is the only solution I can think of as of today

    #standardSQL
    DECLARE var_date_option STRING DEFAULT 'DAY';
    DECLARE start_date, end_date DATE;
    DECLARE date_array ARRAY<DATE>;
    
    SET (start_date, end_date, var_date_option) = ('2019-01-01','2020-01-01', 'MONTH');
    
    SET date_array = (
      SELECT CASE var_date_option 
        WHEN 'DAY' THEN GENERATE_DATE_ARRAY(start_date, end_date, INTERVAL 1 DAY)
        WHEN 'WEEK' THEN GENERATE_DATE_ARRAY(start_date, end_date, INTERVAL 1 WEEK)
        WHEN 'MONTH' THEN GENERATE_DATE_ARRAY(start_date, end_date, INTERVAL 1 MONTH)
      END
    );
    
    SELECT * FROM UNNEST(date_array) AS date_dt;
    
    0 讨论(0)
提交回复
热议问题