GMV归因分解分析

SQL数据分析入门SQL

问题

对GMV进行多维归因分解,分析GMV变化由哪些因素驱动(流量、转化率、客单价、品类、渠道等)。

答题要点

-- GMV归因分析
WITH gmv_current AS (
    SELECT
        DATE_FORMAT(order_date, '%Y-%m') AS month,
        channel,
        category,
        COUNT(DISTINCT user_id) AS users,
        COUNT(DISTINCT order_id) AS orders,
        SUM(amount) AS gmv,
        SUM(amount) / COUNT(DISTINCT order_id) AS avg_order_value,
        COUNT(DISTINCT order_id) / COUNT(DISTINCT user_id) AS purchase_rate
    FROM orders
    WHERE status = 'paid'
        AND order_date >= DATE_SUB(CURDATE(), INTERVAL 2 MONTH)
    GROUP BY DATE_FORMAT(order_date, '%Y-%m'), channel, category
),
gmv_last_month AS (
    SELECT
        DATE_FORMAT(DATE_ADD(order_date, INTERVAL 1 MONTH), '%Y-%m') AS month,
        channel,
        category,
        COUNT(DISTINCT user_id) AS users_last,
        COUNT(DISTINCT order_id) AS orders_last,
        SUM(amount) AS gmv_last,
        SUM(amount) / COUNT(DISTINCT order_id) AS aov_last,
        COUNT(DISTINCT order_id) / COUNT(DISTINCT user_id) AS purchase_rate_last
    FROM orders
    WHERE status = 'paid'
        AND order_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
        AND order_date < DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
    GROUP BY DATE_FORMAT(DATE_ADD(order_date, INTERVAL 1 MONTH), '%Y-%m'), channel, category
),
-- 归因计算
attribution AS (
    SELECT
        c.month,
        c.channel,
        c.category,
        c.gmv,
        COALESCE(l.gmv_last, 0) AS gmv_last,
        c.gmv - COALESCE(l.gmv_last, 0) AS gmv_change,
        ROUND(100.0 * (c.gmv - COALESCE(l.gmv_last, 0)) / NULLIF(l.gmv_last, 0), 2) AS gmv_change_pct,

        -- 归因拆解:GMV = 用户数 × 购买率 × 客单价
        c.users,
        COALESCE(l.users_last, 0) AS users_last,
        c.users - COALESCE(l.users_last, 0) AS users_change,

        c.purchase_rate,
        COALESCE(l.purchase_rate_last, 0) AS purchase_rate_last,
        c.purchase_rate - COALESCE(l.purchase_rate_last, 0) AS purchase_rate_change,

        c.avg_order_value,
        COALESCE(l.aov_last, 0) AS aov_last,
        c.avg_order_value - COALESCE(l.aov_last, 0) AS aov_change,

        -- 各因素贡献度(简化计算)
        (c.users - COALESCE(l.users_last, 0)) * c.purchase_rate * c.avg_order_value AS user_contribution,
        COALESCE(l.users_last, 0) * (c.purchase_rate - COALESCE(l.purchase_rate_last, 0)) * c.avg_order_value AS rate_contribution,
        COALESCE(l.users_last, 0) * COALESCE(l.purchase_rate_last, 0) * (c.avg_order_value - COALESCE(l.aov_last, 0)) AS aov_contribution

    FROM gmv_current c
    LEFT JOIN gmv_last_month l
        ON c.month = l.month
        AND c.channel = l.channel
        AND c.category = l.category
)
SELECT
    month,
    channel,
    category,
    gmv,
    gmv_last,
    gmv_change,
    gmv_change_pct,
    users_change,
    purchase_rate_change,
    aov_change,
    ROUND(user_contribution, 2) AS user_contribution,
    ROUND(rate_contribution, 2) AS rate_contribution,
    ROUND(aov_contribution, 2) AS aov_contribution,
    ROUND(100.0 * user_contribution / NULLIF(gmv_change, 0), 2) AS user_contribution_pct,
    ROUND(100.0 * rate_contribution / NULLIF(gmv_change, 0), 2) AS rate_contribution_pct,
    ROUND(100.0 * aov_contribution / NULLIF(gmv_change, 0), 2) AS aov_contribution_pct
FROM attribution
WHERE gmv > 0
ORDER BY ABS(gmv_change) DESC
LIMIT 50;

-- 渠道贡献度排名
SELECT
    month,
    channel,
    SUM(gmv) AS channel_gmv,
    SUM(gmv_change) AS channel_gmv_change,
    ROUND(100.0 * SUM(gmv) / SUM(SUM(gmv)) OVER (PARTITION BY month), 2) AS gmv_share,
    ROUND(100.0 * SUM(gmv_change) / SUM(SUM(gmv)) OVER (PARTITION BY month), 2) AS change_contribution
FROM attribution
GROUP BY month, channel
ORDER BY month DESC, channel_gmv DESC;

完整解析在题库里看

核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。

去练这道题

回知识库看这一块

同一能力的其他题