fix: 刷新mv_overview_monthly,修复实收口径不一致
mv_overview_monthly是普通表不是物化视图,需要DELETE+INSERT更新 之前未刷新导致/overview API返回旧数据(6083万 vs bill_fact 6441万) 刷新脚本加入mv_overview_monthly更新步骤
This commit is contained in:
@@ -24,6 +24,23 @@ psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" <<EOF
|
||||
REFRESH MATERIALIZED VIEW analytics.bill_fact;
|
||||
SELECT 'bill_fact refreshed' AS status;
|
||||
|
||||
-- 第一层b:总览汇总表(普通表,需DELETE+INSERT)
|
||||
DELETE FROM analytics.mv_overview_monthly WHERE month = '${MONTH_START}';
|
||||
INSERT INTO analytics.mv_overview_monthly
|
||||
SELECT
|
||||
date_trunc('month', closed_at)::date AS month,
|
||||
count(*) AS bill_count,
|
||||
round(sum(received_total)::numeric, 2) AS received,
|
||||
round(avg(received_total)::numeric, 2) AS avg_bill_value,
|
||||
round(avg(CASE WHEN consumption > 0 THEN discount_total::numeric / consumption * 100 ELSE 0 END)::numeric, 2) AS discount_rate_pct,
|
||||
0 AS theoretical_margin_pct,
|
||||
0 AS member_bills,
|
||||
0 AS member_share_pct
|
||||
FROM analytics.bill_fact
|
||||
WHERE closed_at >= '${MONTH_START}' AND closed_at < ('${MONTH_START}'::date + interval '1 month')
|
||||
GROUP BY date_trunc('month', closed_at);
|
||||
SELECT 'mv_overview_monthly updated' AS status;
|
||||
|
||||
-- 第二层:门店评估
|
||||
REFRESH MATERIALIZED VIEW analytics.mv_store_risk_rating_monthly;
|
||||
REFRESH MATERIALIZED VIEW analytics.mv_store_theoretical_actual_cost_monthly;
|
||||
|
||||
Reference in New Issue
Block a user