码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-19 10:24:59      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:The   div   无法   creat   span   upd   erp   group   img   

#控奶新客: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。春演的时候,别的门店店员过来帮忙。注册的门店既不能算本门店,也不可能算原来门店。所以为空           

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

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

技术图片

 

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

标签:The   div   无法   creat   span   upd   erp   group   img   

原文地址:https://www.cnblogs.com/sakura3/p/10399397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!