I want to truncate my table once a month, otherwise it only deletes data from the past 3 months, how can I conditionally truncate or delete?

huangapple go评论64阅读模式
英文:

I want to truncate my table once a month, otherwise it only deletes data from the past 3 months, how can I conditionally truncate or delete?

问题

我有这个变量

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;
英文:

I have this variable

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
);

And it's only true when it is the last friday of every month. I have a script that deletes part of a table depending on the date.

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;

How can I use the is_full_refresh variable as a case statement to be like this or is this already possible and I am just missing a select statement?

Case when is_full_refresh = true then truncate sales_Table else DELETE FROM sales_table
    WHERE date >= $process_from_date;

答案1

得分: 2

你可以使用Snowflake脚本逻辑来实现这个功能:

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;
英文:

You can use Snowflake Scripting logic for this:

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;

huangapple
  • 本文由 发表于 2023年8月4日 04:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定