分析跑数口径与表内在关系逻辑

Posted sakura3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分析跑数口径与表内在关系逻辑相关的知识,希望对你有一定的参考价值。

#控奶新客:T-14至T-7内购买大于等于1,且T-14至T-194(最远到7月)未购控奶
#T为1月13日;
DELETE
FROM
    `0114-0120控奶新客`;

#写入会员基本信息
INSERT INTO `0114-0120控奶新客` (
    门店ID,
    门店,
    会员姓名,
    手机号码,
    控奶首购时间
) SELECT
    *
FROM
    (
        SELECT
            a.salesdepart_id AS 门店ID,
            b.`name` AS 门店,
            c.`name` AS 会员姓名,
            c.mobile AS 手机号码,
            MIN(d.create_date) AS `控奶首购时间`
        FROM
            arm_changsha.goods_sales AS a
        LEFT JOIN arm_changsha.sys_office AS b ON b.id = a.salesdepart_id
        LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = a.member_id
        LEFT JOIN arm_changsha.goods_sales_detail AS d ON d.sales_id = a.id
        LEFT JOIN arm_changsha.goods_base AS e ON e.id = d.goods_id
        WHERE
            a.sales_type = 1
        AND d.barcode IN (
            SELECT
                条码
            FROM
                `tmp_通货`
            WHERE
                商品标签 = 非通货
            AND 大类 LIKE 01%
        )
        AND c.mobile NOT IN (
            SELECT
                mobile
            FROM
                `7~9月英克系统消费客户统计`
        )
        AND c.mobile IS NOT NULL
        GROUP BY
            c.mobile
    ) AS L
WHERE
    `控奶首购时间` >= 2018-12-31 00:00:00
AND `控奶首购时间` <= 2019-01-06 23:59:59;

#写入首购购买数量
UPDATE `0114-0120控奶新客` AS a
SET a.`控奶首购数量` = (
    SELECT
        sum(b.sales_number) AS 购买数量
    FROM
        arm_changsha.goods_sales_detail AS b
    LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id
    LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id
    LEFT JOIN arm_changsha.goods_base AS e ON e.id = b.goods_id
    WHERE
        c.mobile = a.手机号码
    AND b.create_date = a.控奶首购时间
    AND e.kind_id LIKE 01%
    AND d.sales_type = 1
    AND b.barcode IN (
        SELECT
            条码
        FROM
            `tmp_通货`
        WHERE
            商品标签 = 非通货
        AND 大类 LIKE 01%
    )
);

#写入T+7控货奶粉购买数量
UPDATE `0114-0120控奶新客` AS a
SET a.`T+7控奶购买数量` = (
    SELECT
        sum(b.sales_number) AS 购买数量
    FROM
        arm_changsha.goods_sales_detail AS b
    LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id
    LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id
    WHERE
        c.mobile = a.手机号码
    AND b.create_date >= 2019-01-21 00:00:00
    AND b.create_date <= 2019-01-27 23:59:59
    AND d.sales_type = 1
    AND b.barcode IN (
        SELECT
            条码
        FROM
            `tmp_通货`
        WHERE
            商品标签 = 非通货
        AND 大类 LIKE 01%
    )
);

#写入T+7复购金额
UPDATE `0114-0120控奶新客` AS a
SET a.`T+7复购金额` = (
    SELECT
        ROUND(SUM(b.real_pay) / 10000, 2) AS 累计消费金额
    FROM
        arm_changsha.goods_sales AS b
    LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id
    WHERE
        c.mobile = a.手机号码
    AND b.create_date >= 2019-01-21 00:00:00
    AND b.create_date <= 2019-01-27 23:59:59
    AND b.sales_type = 1
    GROUP BY
        b.member_id
);

#写入T+7购买次数
UPDATE `0114-0120控奶新客` AS a
SET a.`T+7小票数` = (
    SELECT
        COUNT(DISTINCT b.sales_id)
    FROM
        arm_changsha.goods_sales_detail AS b
    LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id
    LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id
    WHERE
        c.mobile = a.手机号码
    AND b.create_date >= 2019-01-21 00:00:00
    AND b.create_date <= 2019-01-27 23:59:59
    AND d.sales_type = 1
);

#写入近90日充值金额
UPDATE `0114-0120控奶新客` AS a
SET a.`近90日充值金额` = (
    SELECT
        ROUND(
            sum(b.use_recharge_balance) / 10000,
            2
        ) AS 充值金额
    FROM
        arm_changsha.ms_member_card_log AS b
    LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id
    LEFT JOIN arm_changsha.ms_acct_book_def AS d ON d.member_id = b.member_id
    WHERE
        c.mobile = a.手机号码
    AND d.acct_id IS NOT NULL
    AND b.oper_type IN (4, 7, 8)
    AND b.create_date >= 2018-10-16 00:00:00
    AND b.create_date <= 2019-01-13 23:59:59
);

#写入近90天控货奶粉购买数量
UPDATE `0114-0120控奶新客` AS a
SET a.`近90日控奶购买数量` = (
    SELECT
        sum(b.sales_number) AS 购买数量
    FROM
        arm_changsha.goods_sales_detail AS b
    LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id
    LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id
    WHERE
        c.mobile = a.手机号码
    AND b.create_date >= 2018-10-16 00:00:00
    AND b.create_date <= 2019-01-13 23:59:59
    AND d.sales_type = 1
    AND b.barcode IN (
        SELECT
            条码
        FROM
            `tmp_通货`
        WHERE
            商品标签 = 非通货
        AND 大类 LIKE 01%
    )
);

#判断是否运营门店
UPDATE `0114-0120控奶新客` AS a
SET a.`是否运营门店` = CASE
WHEN a.门店ID IN (
    1008,
    1016,
    1025,
    1026
) THEN
    
ELSE
    
END;

#判断是否稳定
UPDATE `0114-0120控奶新客` AS a
SET a.`是否稳定` = CASE
WHEN a.近90日控奶购买数量 >= 4
OR a.近90日充值金额 >= 500 THEN
    
ELSE
    
END;

 

  1.  为什么以销售流水goods-sales ,作为主表

    答:业务原因有两个。

      1. 客户分两种,有购客户,注册客户。 会员信息表里有一些街上做活动的注册客户,从来没有买过东西。这样无法有效的观测。
      2. 门店信息,原来华创的ERP。春演的时候,别的门店店员过来帮忙。注册的门店既不能算本门店,也不可能算原来门店。所以为空           

      就算不考虑这些业务原因,也要如此去做。

      分析的自然是销售流水,不可能以主表为门店或者会员。都是不符合设计逻辑的

技术图片

 



以上是关于分析跑数口径与表内在关系逻辑的主要内容,如果未能解决你的问题,请参考以下文章

数据库表与表之间的关系

数据模型图解分析(用户订单商品)

数据库之 表与表之间的关系

如何获取片段内在firebase中注册的用户数?

0914 表与表之间的关系补充一对一关系 记录操作 关键字 多对多 子查询

利用Graphviz绘制逻辑关系依赖图