我想每月截断一次表,否则只会删除过去3个月的数据,如何有条件地截断或删除?

问题描述 投票:0回答:1

我有这个变量

set is_full_refresh = (
CASE
WHEN DAY(LAST_DAY(current_date)) - DAY(current_date) < 7 AND DAYOFWEEK(current_date) = 5 THEN TRUE
ELSE FALSE END
);

只有在每个月的最后一个星期五时才如此。我有一个脚本,可以根据日期删除表的一部分。

SET sales_date = (
SELECT MIN(max_date) AS date
FROM (
    SELECT date_type, MAX(date) AS max_date
    FROM sales_table
    GROUP BY date_type
)a);

SET process_from_date = (SELECT COALESCE(DATEADD('MONTH', -3, DATE_TRUNC('MONTH', $sales_date))::DATE, '2019-01-01'::DATE))

DELETE FROM table
WHERE date >= $process_from_date;

如何使用 is_full_refresh 变量作为这样的 case 语句,或者这已经可能,而我只是缺少一个 select 语句?

Case when is_full_refresh = true then truncate sales_Table else DELETE FROM sales_table
    WHERE date >= $process_from_date;
snowflake-cloud-data-platform where-clause case sql-delete truncate
1个回答
0
投票

您可以使用 Snowflake Scripting 逻辑来实现此目的:

BEGIN
    let is_full_refresh boolean := CASE
        WHEN DAY(LAST_DAY(current_date())) - DAY(current_date()) < 7 AND DAYOFWEEK(current_date()) = 5 THEN TRUE
        ELSE FALSE END;
    
    IF (is_full_refresh) THEN
        truncate sales_table;
        return 'truncated table';
    ELSE
         let sales_date TIMESTAMP_NTZ := (
            SELECT MIN(max_date) AS date
            FROM (
                SELECT date_type, MAX(date) AS max_date
                FROM sales_table
                GROUP BY date_type
            )a);
        let process_from_date timestamp_ntz := (SELECT COALESCE(DATEADD('MONTH', -3, DATE_TRUNC('MONTH', :sales_date))::DATE, '2019-01-01'::DATE));
        
        DELETE FROM sales_table WHERE date >= :process_from_date;
        
        return 'deleted records older than ' || :process_from_date::string;
    END IF;
END;
© www.soinside.com 2019 - 2024. All rights reserved.