SQL稿

##查询到店会员人数
SELECT  '0-7天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS B  UNION ALL 
SELECT  '7-15天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS C  UNION ALL 
SELECT  '15-30天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS D  UNION ALL 
SELECT  '30-60天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS E  UNION ALL 
SELECT  '60天以上' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS D;

##查询到店游客人数

SELECT  '0-7天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS B  UNION ALL 
SELECT  '7-15天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS B UNION ALL  
SELECT  '15-30天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS B UNION ALL
SELECT  '30-60天' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS B UNION ALL
SELECT  '60天以上' AS day, COUNT(*) AS count FROM (
        SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')  AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A) GROUP BY  ord.mb_id ) AS B

##会员人数占比
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '0-7天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*)
FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT
        cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是'
    GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '7-15天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*) FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id ) GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '15-30天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*) FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (
    SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '30-60天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*) FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id
)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '60天以上' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*)
FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (
    SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id
)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A 


##游客人数占比
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '0-7天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*)
FROM mmc_order AS ord,mmc_member as mb 
WHERE
    ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (
    SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是'
    GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-08-25'
        AND '2017-08-25' AND ord.st_id = 37 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 35 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37
    AND cd.state = '是' GROUP BY mb.mb_id) GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '7-15天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*) FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 35 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 35
    AND cd.state = '是' GROUP BY mb.mb_id) GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '15-30天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*)
FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 35 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 35
    AND cd.state = '是' GROUP BY mb.mb_id) GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '30-60天' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*)
FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (
    SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
    AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 35 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 35
    AND cd.state = '是' GROUP BY mb.mb_id) GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A UNION ALL
SELECT day,CONCAT(count,'%') as bili FROM(
SELECT '60天以上' AS day, ROUND(COUNT(*)/(SELECT COUNT(*) FROM (
SELECT COUNT(*) FROM mmc_order AS ord,mmc_member as mb 
WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (
    SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
    WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id )
GROUP BY ord.mb_id) as A)*100,2) AS count
FROM ( SELECT  COUNT(mb_id) FROM mmc_order AS ord WHERE substring(ord.ord_create_time, 1, 10) BETWEEN '2017-09-11'
        AND '2017-09-11' AND ord.st_id = 35 AND ord.ord_state in ('待评价','已完成')
AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 35 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡' AND cd.st_id = 35
    AND cd.state = '是' GROUP BY mb.mb_id) GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )  GROUP BY  ord.mb_id ) AS B ) as A ;

##会员消费总额
SELECT '0-7天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  UNION ALL 
SELECT '7-15天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  UNION ALL 
SELECT '15-30天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  UNION ALL 
SELECT '30-60天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  UNION ALL 
SELECT '60天以上' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A )  ;

##游客消费总额
SELECT '0-7天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A ) UNION ALL 
SELECT '7-15天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A ) UNION ALL 
SELECT '15-30天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A ) UNION ALL 
SELECT '30-60天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A ) UNION ALL 
SELECT '60天以上' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A );

#会员消费占比
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '0-7天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '7-15天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '15-30天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '30-60天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '60天以上' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT A.mb_id FROM
    ( SELECT mb.mb_id FROM mmc_member_card AS cd, mmc_member AS mb
        WHERE mb.mb_id = cd.mb_id AND cd.card_type = '会员充值卡'
        AND cd.st_id = 35 AND cd.state = '是' GROUP BY mb.mb_id ) AS A)
GROUP BY ord.mb_id) as A )) as A) as B ;

##游客消费占比
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '0-7天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '7-15天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '15-30天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '30-60天' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )) as A) as B UNION ALL
SELECT day,CONCAT(ROUND(bl*100,2),'%') as bili FROM (
SELECT day,money/(SELECT ROUND(IFNULL(SUM(ord.ord_pay_money),0),2) as total FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')) as bl FROM (
SELECT '60天以上' as day, ROUND(IFNULL(SUM(ord_pay_money),0),2) as money FROM mmc_order as ord where ord.ord_state in ('已完成','待评价') AND ord.ord_type in ('门店下单','购买套餐')
 AND substring(ord.ord_create_time,1,10) BETWEEN '2017-09-11' AND '2017-09-11' AND ord.st_id=35 AND ord.mb_id NOT in (SELECT mb_id FROM (
SELECT ord.mb_id FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN (SELECT mb_id FROM ( SELECT ord.mb_id
FROM mmc_order AS ord,mmc_member as mb  WHERE ord.st_id = 37 AND mb.mb_id=ord.mb_id
AND ord.mb_id NOT IN ( SELECT cd.mb_id
    FROM mmc_member_card AS cd, mmc_member AS mb WHERE mb.mb_id = cd.mb_id
    AND cd.card_type = '会员充值卡' AND cd.st_id = 37 AND cd.state = '是' GROUP BY mb.mb_id)
GROUP BY ord.mb_id) as A) GROUP BY ord.mb_id) as A )) as A) as B ;






你可能感兴趣的:(SQL稿)