CREATE MATERIALIZED VIEW mv_demo
PARTITION BY (pay_date)  -- 关键分区声明
BUILD DEFERRED
REFRESH AUTO ON SCHEDULE EVERY 1 DAY START ('2025-06-27 03:00:00')
DISTRIBUTED BY HASH(game_id, user_id) BUCKETS AUTO
PROPERTIES (
    'replication_num' = '1',
    'partition_refresh_number' = '1',
    'partition_sync_limit' = '1',         -- 仅同步最近1分区
    'partition_sync_time_unit' = 'DAY'    -- 按天同步
)
AS
SELECT
    a.game_id,
    a.user_id,
    b.reg_date,
    a.pay_date,
    a.amount,
    (a.pay_date - b.reg_date) AS rd
FROM (
    SELECT
        game_id,
        user_id,
        pay_date,
        SUM(amount) AS amount
    FROM ods_platform_order
    WHERE status = 1
    GROUP BY game_id, user_id, pay_date
) a
INNER JOIN ods_platform_user b
ON a.user_id = b.user_id AND a.game_id = b.game_id
;