-- 统计系统、品牌、店铺
SELECT DISTINCT source_system FROM dwd_trade_hkaudit_ecommerce_sale_mt;
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'PF_ESP';
SELECT SUBSTR(order_date, 1, 4), brand_code FROM dwd_trade_hkaudit_ecommerce_sale_mt 
	GROUP BY SUBSTR(order_date, 1, 4), brand_code ORDER BY SUBSTR(order_date, 1, 4);

SELECT SUBSTR(order_date, 1, 4), store_code, MAX(store_name) FROM dwd_trade_hkaudit_ecommerce_sale_mt 
	GROUP BY SUBSTR(order_date, 1, 4), store_code ORDER BY SUBSTR(order_date, 1, 4);

SELECT COUNT(DISTINCT store_code) FROM dwd_trade_hkaudit_ecommerce_sale_mt;

-- 赠品有金额
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE is_gift = '是' AND goods_amt > 0;

-- 钱货两清时间24年，支付25年
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE platform_order_no = '3725079539738352640';
SELECT source_system, brand_code , order_time , system_order_no , store_code , store_name , manual_order , is_swap_order , platform_order_no , 
	pay_time , deliver_time , mgclear_time , main_logistic_bill 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time < order_time AND manual_order <> '是';
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time < order_time AND manual_order = '是' AND mgclear_time <> '';
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time = '' AND manual_order = '是';

SELECT store_code , MAX(store_name), sum(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE SUBSTR(mgclear_time, 1, 4)  < '2022' AND brand_code NOT IN ('SBZ', 'YEO') AND order_date < '2025-07-01' AND mgclear_time <> ''
GROUP BY store_code;

SELECT COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE mgclear_time = '' AND order_date < '2025-07-01';
SELECT brand_code,  COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time = ''
GROUP BY brand_code ;
SELECT store_code, SUBSTR(order_date, 1, 7), MAX(store_name), COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time = ''
GROUP BY store_code, SUBSTR(order_date, 1, 7);

SELECT source_system, brand_code, COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time <> '' AND mgclear_time < order_time
GROUP BY source_system, brand_code;

SELECT DISTINCT store_code 
FROM custom_online_sale_order_local
WHERE mgclear_time <> '' AND mgclear_time < order_time
AND source_system = 'SBZE3';
SELECT *
FROM custom_online_sale_order_local
WHERE mgclear_time <> '' AND mgclear_time < order_time
AND source_system = 'SBZE3' AND store_code = 'SW00067';

SELECT source_system, brand_code
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time < order_time AND manual_order <> '是'
GROUP BY source_system, brand_code;


SELECT brand, COUNT() FROM dwd_basic_all_vip_info_dd WHERE member_register_shop = '' GROUP BY brand ; 
SELECT SUBSTR(member_register_time, 1, 4), COUNT()  FROM dwd_basic_all_vip_info_dd WHERE member_register_shop = '' AND brand = 'BES' GROUP BY SUBSTR(member_register_time, 1, 4); 


-- 所有品牌
SELECT DISTINCT brand_code 
FROM dwd_trade_hkaudit_ecommerce_sale_mt;

-- 斯博兹 京东奥莱店铺
SELECT store_code, MAX(store_name), SUM(goods_amt)  
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_name IN ('京东奥莱官方旗舰店', '京东奥莱秒送店')
	AND order_date >= '2024-01-01' AND order_date < '2025-07-01'
GROUP BY store_code;

SELECT store_code, MAX(store_name), SUM(goods_amt)  
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE brand_code = 'SBZ'
	AND order_date >= '2024-01-01' AND order_date < '2025-07-01'
GROUP BY store_code ORDER BY SUM(goods_amt) DESC;


-- 斯博兹按年月排(下单时间)
SELECT SUBSTR(order_date, 1, 7), SUM(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE brand_code = 'SBZ'
GROUP BY SUBSTR(order_date, 1, 7)
ORDER BY SUM(goods_amt);

-- 斯博兹按年月排(钱货两清时间)
SELECT SUBSTR(mgclear_time, 1, 7), SUM(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE brand_code = 'SBZ'
GROUP BY SUBSTR(mgclear_time, 1, 7)
ORDER BY SUM(goods_amt);

-- 各品牌钱货两清时间为空的按照下单时间统计
SELECT brand_code, SUBSTR(order_time, 1, 7), COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE mgclear_time IS NULL OR mgclear_time = ''
GROUP BY brand_code, SUBSTR(order_time, 1, 7);

-- 订单结算金额为负的没有
-- 订单商品结算金额为负 -1102996285.66
SELECT SUM(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE goods_amt < 0;
-- 具体数据
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE goods_amt < 0 LIMIT 1000;
-- 按照店铺分
SELECT store_code, MAX(store_name), SUM(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE goods_amt < 0
GROUP BY store_code;

-- 店铺编码对应多个名称 176家存在
SELECT store_code, COUNT(DISTINCT store_name) 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
GROUP BY store_code HAVING COUNT(DISTINCT store_name) > 1;

-- 线上销售均摊金额之和是否等于订单结算金额
SELECT a.platform_order_no, a.order_amount, b.goods_amount FROM
(SELECT platform_order_no, MAX(order_settle_amt) AS order_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE platform_order_no <> '' AND platform_order_no IS NOT NULL
GROUP BY platform_order_no) a
LEFT JOIN (SELECT platform_order_no, SUM(goods_amt) AS goods_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
GROUP BY platform_order_no) b ON a.platform_order_no = b.platform_order_no
WHERE a.order_amount <> b.goods_amount;

SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE platform_order_no = '284444210101';

-- 换货单查询
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE is_swap_order <> '否';
-- 为空的 4912136
SELECT COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE is_swap_order = '';

-- 下单和发货间隔
-- 发货时间为空的 手工单 41 总共 14193791
SELECT
    COUNT()
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE manual_order = '是' AND deliver_time = '';
SELECT
    COUNT()
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE deliver_time = '';
-- 发货时间为空的数据查询
SELECT
    store_code, MAX(store_name), COUNT()  
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE deliver_time = '' AND SUBSTR(order_date, 1, 4) = '2024'
AND store_name NOT LIKE '%唯品会%' AND store_name NOT LIKE '%京东自营%' AND brand_code <> 'SBZ'
GROUP BY store_code ;
-- 按店铺各月发货时间为空分布
SELECT
    store_code, MAX(store_name), SUBSTR(order_date, 1, 7), COUNT()  
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE deliver_time = '' AND store_name NOT LIKE '%唯品会%'
GROUP BY store_code, SUBSTR(order_date, 1, 7);

SELECT
    store_code, MAX(store_name), source_platform , COUNT(), COUNT(DISTINCT platform_order_no), COUNT(DISTINCT system_order_no)  
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE deliver_time = '' AND store_name NOT LIKE '%唯品会%'
GROUP BY store_code, source_platform ;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_code = 'SF00242';

SELECT
    *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE deliver_time = '' AND SUBSTR(order_date, 1, 4) = '2024' AND store_code IN ('S000001');
-- 发货间隔  3天以上 44507   >7 30796  > 15 23021  >30 20540
SELECT
    COUNT()
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE manual_order = '是' AND deliver_time <> '' AND dateDiff('day', toDateTime(order_time), toDateTime(deliver_time)) > 30;
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE manual_order = '是' AND deliver_time <> '' AND dateDiff('day', toDateTime(order_time), toDateTime(deliver_time)) > 30;
SELECT AVG(dateDiff('day', toDateTime(order_time), toDateTime(deliver_time)))
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE manual_order <> '是' AND deliver_time <> '';

-- 粗略计算销售金额
SELECT
    SUM(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE mgclear_time <> '' AND YEAR(toDate(mgclear_time)) = '2024';
SELECT
    SUM(return_goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE mgclear_time <> '' AND YEAR(toDate(mgclear_time)) = '2024';
SELECT
    SUM(special_barcode_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_change_mt WHERE mgclear_time <> '' AND YEAR(toDate(mgclear_time)) = '2024';

-- 每个店铺每年收入情况
-- 不同店铺相同订单号 802
SELECT platform_order_no 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
GROUP BY platform_order_no HAVING COUNT(DISTINCT store_code) > 1;

SELECT * 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE platform_order_no = '4155933925640151503';
-- 带入店铺算
SELECT a.perYear, a.store_code, MAX(a.store_name), SUM(a.sale_amount), SUM(b.sale_refund_amount), SUM(c.sale_change_amount)
FROM (SELECT
    SUBSTR(order_date, 1, 4) AS perYear,
	store_code,
	MAX(store_name) AS store_name,
	platform_order_no,
	SUM(goods_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
--WHERE SUBSTR(order_date, 1, 4) = '2024'
GROUP BY SUBSTR(order_date, 1, 4), store_code, platform_order_no) a
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(return_goods_amt) AS sale_refund_amount
	FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
	GROUP BY store_code, platform_order_no
) b ON a.store_code = b.store_code AND a.platform_order_no = b.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(special_barcode_amt) AS sale_change_amount
	FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
	GROUP BY store_code, platform_order_no
) c ON a.store_code = c.store_code AND a.platform_order_no = c.platform_order_no
GROUP BY a.perYear, a.store_code;

SELECT a.perYear, a.store_code, MAX(a.store_name), SUM(a.sale_amount), SUM(b.sale_refund_amount), SUM(c.sale_change_amount)
FROM (SELECT
    SUBSTR(order_date, 1, 4) AS perYear,
	store_code,
	MAX(store_name) AS store_name,
	platform_order_no,
	SUM(goods_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE order_date >= '2025-01-01' AND order_date < '2025-07-01'
GROUP BY SUBSTR(order_date, 1, 4), store_code, platform_order_no) a
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(return_goods_amt) AS sale_refund_amount
	FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
	GROUP BY store_code, platform_order_no
) b ON a.store_code = b.store_code AND a.platform_order_no = b.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(special_barcode_amt) AS sale_change_amount
	FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
	GROUP BY store_code, platform_order_no
) c ON a.store_code = c.store_code AND a.platform_order_no = c.platform_order_no
GROUP BY a.perYear, a.store_code;

SELECT a.perYear, a.store_code, MAX(a.store_name), SUM(a.sale_amount), SUM(b.sale_refund_amount), SUM(c.sale_change_amount)
FROM (SELECT
    SUBSTR(order_date, 1, 4) AS perYear,
	store_code,
	MAX(store_name) AS store_name,
	platform_order_no,
	SUM(goods_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE SUBSTR(order_date, 1, 4) = '2024' AND store_code = 'DHF3'
GROUP BY SUBSTR(order_date, 1, 4), store_code, platform_order_no) a
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(return_goods_amt) AS sale_refund_amount
	FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
	GROUP BY store_code, platform_order_no
) b ON a.store_code = b.store_code AND a.platform_order_no = b.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(special_barcode_amt) AS sale_change_amount
	FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
	GROUP BY store_code, platform_order_no
) c ON a.store_code = c.store_code AND a.platform_order_no = c.platform_order_no
GROUP BY a.perYear, a.store_code;

-- 发货调整数量不为0的
SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
WHERE goods_qty <> 0 AND SUBSTR(create_date, 1, 4) = '2023';

SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
WHERE system_order_no = 'BS513100011188';

-- 销售有负数  退销有正数
SELECT source_system, COUNT()  FROM 
dwd_trade_hkaudit_ecommerce_sale_mt WHERE goods_amt < 0
GROUP BY source_system ;
SELECT MAX(source_system) , store_code, MAX(store_name), COUNT()  FROM 
dwd_trade_hkaudit_ecommerce_sale_mt WHERE goods_amt < 0
GROUP BY store_code ;

SELECT COUNT() 
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
WHERE return_goods_amt > 0;

SELECT *
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
WHERE system_order_no = 'BS210100000263';

SELECT source_system, COUNT()  FROM 
dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no LIKE 'FHTZ%'
GROUP BY source_system;

SELECT DISTINCT store_name 
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE source_system = 'PF_ECP';

-- 订单重复行计算 140579734  
SELECT COUNT(DISTINCT *) FROM  dwd_trade_hkaudit_ecommerce_sale_mt;
-- 145002782
SELECT COUNT() FROM  dwd_trade_hkaudit_ecommerce_sale_mt;
-- 取数查看
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE system_order_no IN (
	SELECT system_order_no FROM dwd_trade_hkaudit_ecommerce_sale_mt
	WHERE system_order_no IN (
		SELECT system_order_no
		FROM dwd_trade_hkaudit_ecommerce_sale_mt
		WHERE source_system = 'E3PLUS_NEW'
		GROUP BY system_order_no, platform_order_no, goods_barcode, goods_amt
		HAVING COUNT() > 1 
	)
	GROUP BY system_order_no HAVING MAX(order_settle_amt) < SUM(goods_amt)
) ORDER BY system_order_no DESC;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE system_order_no = 'BS4A0300002724';

-- 退销平台单号查看（是否和销售单号关联）
SELECT SUM(platform_order_num)
FROM (SELECT store_code, COUNT(DISTINCT platform_order_no) AS platform_order_num
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE SUBSTR(create_time, 1, 10)  >= '2025-01-01' AND SUBSTR(create_time, 1, 10)  < '2025-07-01' 
GROUP BY store_code);

SELECT SUM(platform_order_num)
FROM (
	SELECT store_code, COUNT(DISTINCT platform_order_no) AS platform_order_num
	FROM (SELECT *
		FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
		WHERE SUBSTR(create_time, 1, 10)  >= '2025-01-01' AND SUBSTR(create_time, 1, 10)  < '2025-03-01' ) a
	INNER JOIN (SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE SUBSTR(order_date, 1, 4) = '2025') b ON a.store_code = b.store_code AND a.platform_order_no = b.platform_order_no
	GROUP BY store_code
);

SELECT source_platform, store_code, MAX(store_name), SUM(goods_amt)
FROM dwd_trade_hkaudit_ecommerce_sale_mt
GROUP BY source_platform, store_code ORDER BY SUM(goods_amt) DESC;

-- 账单：BS3B1900003767  BS392000041139
SELECT * FROM dwd_trade_hkaudit_ecommerce_account_mt dtheam  WHERE bill_source = '1' AND SUBSTR(transaction_date, 1, 4) = '2024' AND matching_status  = '1';
SELECT * FROM dwd_trade_hkaudit_ecommerce_account_mt dtheam  WHERE matching_no = 'BS3923R0002529';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE platform_order_no LIKE '%231119-419587705691524%';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE system_order_no  = 'BS392000041139';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt dthesrm WHERE platform_order_no LIKE '%,%';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt dthesrm WHERE system_order_no = 'BS3923R0002529';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_change_mt dthescm  WHERE platform_order_no IN ('231119-071838012891524,231119-419587705691524');

-- 18,615,790
SELECT COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code = 'DHF3' AND mgclear_time <> '' AND is_gift <> '是';
-- 14,621,773   14,641,473
SELECT COUNT(DISTINCT matching_no) FROM dwd_trade_hkaudit_ecommerce_account_mt WHERE matching_no IN (
	SELECT DISTINCT system_order_no FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code = 'DHF3' AND mgclear_time <> '' 
);
-- 
SELECT COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code = 'DHF3' AND mgclear_time <> '' AND is_gift <> '是' AND system_order_no IN (
	SELECT DISTINCT matching_no FROM dwd_trade_hkaudit_ecommerce_account_mt
);

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code = 'DHF3' AND mgclear_time <> '' AND is_gift <> '是' AND system_order_no NOT IN (
	SELECT DISTINCT matching_no FROM dwd_trade_hkaudit_ecommerce_account_mt
);
-- TM490900000511
SELECT * FROM dwd_trade_hkaudit_ecommerce_account_mt WHERE matching_no = 'TM461000000493';
SELECT * FROM dwd_trade_hkaudit_ecommerce_account_mt WHERE transaction_no = '2185819176131246881';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no LIKE '%4037358494865750517%';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no  = 'TM461000000493';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE association_order_no = 'TM461000000493';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no LIKE '%4037358494865750517%';

SELECT * FROM dwd_trade_hkaudit_ecommerce_account_mt dtheam  WHERE transaction_no = '2520559669307517693';

-- 两边已两清的，账单应收的
SELECT 
	SUM(a.sale_amount), 
	SUM(b.sale_refund_amount),
	SUM(c.amount_sum),
	SUM(d.order_freight_amt),
	SUM(f.sale_change_amount)
FROM (SELECT
		platform_order_no,
		SUM(goods_amt) AS sale_amount
	FROM custom_online_sale_local
	WHERE store_code = 'DHF3' AND mgclear_time <> '' AND order_date = '2024-04-13'
	GROUP BY platform_order_no) a
LEFT JOIN (
	SELECT platform_order_no, SUM(return_goods_amt) AS sale_refund_amount
	FROM custom_online_sale_return_local
	WHERE store_code = 'DHF3'
	GROUP BY platform_order_no
) b ON a.platform_order_no = b.platform_order_no
LEFT JOIN (
	SELECT transaction_no, SUM(amount) AS amount_sum
	FROM dwd_trade_hkaudit_ecommerce_account_mt
	WHERE shop_code = 'DHF3' AND bill_type = '1' AND quits_status = '1'
	GROUP BY transaction_no
) c ON a.platform_order_no = c.transaction_no
LEFT JOIN (
	SELECT platform_order_no, SUM(order_freight_amt_t) AS order_freight_amt
	FROM (SELECT
			system_order_no,
			platform_order_no,
			MAX(order_freight_amt) AS order_freight_amt_t
		FROM custom_online_sale_local
		WHERE store_code = 'DHF3' AND mgclear_time <> '' AND order_date = '2024-04-13' AND order_freight_amt > 0
		GROUP BY system_order_no, platform_order_no
	) GROUP BY platform_order_no
) d ON a.platform_order_no = d.platform_order_no
LEFT JOIN (
	SELECT platform_order_no, SUM(special_barcode_amt) AS sale_change_amount
	FROM custom_online_sale_change_local
	WHERE store_code = 'DHF3' 
	GROUP BY platform_order_no
) f ON a.platform_order_no = f.platform_order_no;

SELECT COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE order_date < '2022-01-01';

SELECT source_system, COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_mt 
WHERE order_date < '2022-01-01' GROUP BY source_system;

SELECT brand_code, COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_mt 
WHERE order_date < '2022-01-01' AND source_system = 'E3PLUS_NEW2' GROUP BY brand_code;

SELECT COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'EC_HIS' AND store_code = 'DHF3';

SELECT brand_code, SUBSTR(create_date, 1, 7), COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_return_mt dthesrm  WHERE platform_order_no IN (
SELECT platform_order_no FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE mgclear_time <> '' GROUP BY platform_order_no)
AND mgclear_time = '' AND brand_code = 'HLA' GROUP BY brand_code, SUBSTR(create_date, 1, 7) ;

SELECT COUNT() FROM dwd_trade_hkaudit_ecommerce_sale_mt 
WHERE source_system = 'SBZ' AND platform_order_no IN (
	SELECT platform_order_no FROM dwd_trade_hkaudit_ecommerce_sale_mt 
	WHERE source_system = 'SBZ_HIS'
);


-- 结算不等于商品总额的
SELECT * FROM 
(SELECT store_code, system_order_no, MAX(order_settle_amt) AS order_settle_amt, MAX(order_freight_amt) AS order_freight_amt
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE source_system NOT LIKE 'EC%' AND store_name NOT LIKE '%唯品会%' AND store_name NOT LIKE '%京东自营%' AND store_name NOT LIKE '%英式%'
GROUP BY store_code, system_order_no
) a LEFT JOIN (
	SELECT store_code, system_order_no, SUM(goods_amt) AS goods_amt
	FROM dwd_trade_hkaudit_ecommerce_sale_mt
	WHERE source_system NOT LIKE 'EC%' AND store_name NOT LIKE '%唯品会%' AND store_name NOT LIKE '%京东自营%' AND store_name NOT LIKE '%英式%'
	GROUP BY store_code, system_order_no
) b ON a.store_code = b.store_code AND a.system_order_no = b.system_order_no
WHERE a.order_settle_amt <> b.goods_amt ;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no = '3201016386813';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no = '3203069261697';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no = 'BS282100018218';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no LIKE '%235316176071%';

SELECT system_order_no FROM  dwd_trade_hkaudit_ecommerce_sale_mt GROUP BY system_order_no HAVING MAX(order_settle_amt) <> 0 AND SUM(goods_amt) = 0;



SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no IN
(SELECT platform_order_no FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE is_swap_order = '是' AND store_name LIKE '%京东%' AND brand_code <> 'SBZ')
ORDER BY platform_order_no;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE platform_order_no = '213591262551';


SELECT DISTINCT store_code , store_name  FROM dim_hkaudit_store_mt WHERE brand_code  = 'HLA'  AND store_name LIKE '%京东%' ; 

SELECT DISTINCT store_code, store_name  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code IN (
SELECT DISTINCT store_code  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE  goods_amt  < 0 AND store_name NOT LIKE '%唯品会%' AND store_name NOT LIKE '%京东自营%')
ORDER BY platform_order_no ;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code = 'DYQ2';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE  goods_amt  < 0 AND source_system LIKE '%E3%';


SELECT source_system, COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no = '' GROUP BY source_system;

PF_ECP_NEW2	5190094
PF_OCP_NEW	541202
PF_ESP_NEW	553009
SBZE3	7648558
PF_YEP_NEW	1079740

PF_ECP_NEW2	5190094
SBZE3	5093236
PF_OCP_NEW	541202
PF_YEP_NEW	1079740
PF_ESP_NEW	553009

SELECT SUBSTR(system_order_no, 1, 7), COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZE3' AND platform_order_no = '' GROUP BY SUBSTR(system_order_no, 1, 7);

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE brand_code  = 'SBZ' AND platform_order_no = '' AND system_order_no LIKE '%XSDD%';


SELECT store_code  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system <> 'EC_HIS_NEW' GROUP BY store_code HAVING COUNT(DISTINCT source_system) > 1 ;
SELECT store_code, MAX(store_name), arrayStringConcat(groupArray(distinct source_system), ',') AS source_system_m FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system LIKE 'PF%'
GROUP BY store_code HAVING COUNT(DISTINCT source_system) > 1 ;

SELECT DISTINCT source_system  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system LIKE 'PF%' AND store_code = 'DYO9';

SELECT DISTINCT brand_code FROM dwd_trade_hkaudit_ecommerce_sale_mt;


SELECT DISTINCT source_system  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code  = 'DYE2';
SELECT *  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code  = 'DYE2';
SELECT *  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no  = '22011819700880';


SELECT source_system, manual_order, COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE mgclear_time <> '' AND mgclear_time < order_time GROUP BY source_system, manual_order ;
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE mgclear_time <> '' AND mgclear_time < order_time AND source_system = 'PF_ESP_NEW';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE mgclear_time <> '' AND mgclear_time < order_time AND source_system IN ('E3PLUS_NEW2','EC_HIS_NEW')
	AND dateDiff('day', toDateTime(mgclear_time), toDateTime(order_time)) > 1;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE deliver_time  < order_time AND source_system IN ('E3PLUS_NEW2','EC_HIS_NEW')
	AND dateDiff('day', toDateTime(deliver_time), toDateTime(order_time)) > 1;
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE deliver_time  < order_time AND source_system IN ('E3PLUS_NEW2','EC_HIS_NEW') AND manual_order = '';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE SUBSTR(order_date, 1, 4) = '2121';

-- 346691892 , 246212129
SELECT *  FROM dwd_basic_all_vip_info_dd dbavid WHERE member_register_shop = '';
SELECT * FROM dwd_basic_all_vip_info_dd WHERE member_id IN (SELECT member_id FROM dwd_basic_all_vip_info_dd GROUP BY member_id HAVING COUNT() > 1) ORDER BY member_id ;

SELECT * FROM dwd_basic_all_vip_info_dd WHERE member_register_shop = '' AND brand  = 'HLA' ORDER BY member_register_time desc;

SELECT bill_no, COUNT() FROM dwd_basic_all_vip_point_dd dbavid GROUP BY bill_no HAVING COUNT() > 1 order by COUNT() DESC;

SELECT * FROM dwd_basic_all_vip_point_dd dbavid WHERE bill_no = '01H3HP2311120001';
SELECT * FROM dwd_basic_all_vip_point_dd dbavid WHERE bill_no = '01H3HP2311120001';
SELECT * FROM dwd_basic_all_vip_point_dd dbavid WHERE bill_no LIKE 'BS%';

SELECT DISTINCT substr(system_order_no, 1, 2) FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE source_system = 'SBZE3';
SELECT DISTINCT substr(system_order_no, 1, 4) FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE source_system = 'SBZE3' AND substr(system_order_no, 1, 2) IN ('XS', 'JX');

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE substr(system_order_no, 1, 5) = 'XSJSD';


SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no = '';


SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE source_system = 'SBZE3' AND create_date = '2025-03-28' AND return_goods_code = 'JP98143.5';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZE3' AND order_date = '2025-03-28' AND goods_qty < 0;

SELECT COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE source_system = 'SBZE3' AND SUBSTR(system_order_no, 1, 5) = 'XSJSD' AND return_goods_qty < 0;

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE SUBSTR(order_date, 1, 4) > '2100';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt  WHERE system_order_no = '12012533354151';

SELECT COUNT() FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system LIKE 'PF%' AND SUBSTR(order_time, 1, 4) < '2022'; 

SELECT DISTINCT store_name FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_name LIKE '%微商城%';

-- 闭店还有单子产生吗
-- 相差时间间隔


SELECT store_code, brand_code, COUNT(DISTINCT system_order_no), SUM(order_settle_amt_t) 
FROM (SELECT store_code, brand_code, system_order_no, MAX(order_settle_amt) AS order_settle_amt_t FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE source_system LIKE 'E3PLUS%' AND mgclear_time <> '' AND SUBSTR(mgclear_time, 1, 10) >= '2022-01-01' AND SUBSTR(mgclear_time, 1, 10) < '2025-07-01'
GROUP BY store_code, brand_code, system_order_no
) GROUP BY store_code, brand_code
ORDER BY brand_code, store_code;

SELECT store_code, brand_code, COUNT(DISTINCT system_order_no), SUM(order_settle_amt_t) 
FROM (SELECT store_code, brand_code, system_order_no, MAX(order_settle_amt) AS order_settle_amt_t FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE source_system LIKE 'E3PLUS%' AND SUBSTR(order_time, 1, 10) < '2025-07-01'
GROUP BY store_code, brand_code, system_order_no
) GROUP BY store_code, brand_code
ORDER BY brand_code, store_code;

SELECT DISTINCT system_order_no FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE store_code = 'DHZQ' AND source_system LIKE 'E3PLUS%' AND SUBSTR(order_time, 1, 10) < '2025-07-01';

SELECT store_code, brand_code, COUNT(DISTINCT system_order_no), SUM(return_goods_amt_t) 
FROM (SELECT store_code, brand_code, system_order_no, SUM(return_goods_amt) AS return_goods_amt_t FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
WHERE source_system LIKE 'E3PLUS%' AND SUBSTR(create_time, 1, 10) < '2025-07-01'
GROUP BY store_code, brand_code, system_order_no
) GROUP BY store_code, brand_code
ORDER BY brand_code, store_code;

-- EC
SELECT store_code, brand_code, COUNT(DISTINCT system_order_no), SUM(order_settle_amt_t) 
FROM (SELECT store_code, brand_code, system_order_no, MAX(order_settle_amt) AS order_settle_amt_t FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE source_system LIKE 'EC%' AND mgclear_time <> '' AND SUBSTR(mgclear_time, 1, 10) >= '2022-01-01' AND SUBSTR(mgclear_time, 1, 10) < '2025-07-01'
GROUP BY store_code, brand_code, system_order_no
) GROUP BY store_code, brand_code
ORDER BY brand_code, store_code;

SELECT store_code, brand_code, COUNT(DISTINCT system_order_no), SUM(return_goods_amt_t) 
FROM (SELECT store_code, brand_code, system_order_no, SUM(return_goods_amt) AS return_goods_amt_t FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
WHERE source_system LIKE 'EC%' AND mgclear_time <> '' AND SUBSTR(mgclear_time, 1, 10) >= '2022-01-01' AND SUBSTR(mgclear_time, 1, 10) < '2025-07-01'
GROUP BY store_code, brand_code, system_order_no
) GROUP BY store_code, brand_code
ORDER BY brand_code, store_code;

SELECT store_code, brand_code, COUNT(DISTINCT system_order_no), SUM(special_barcode_amt_t) 
FROM (SELECT store_code, brand_code, system_order_no, SUM(special_barcode_amt) AS special_barcode_amt_t FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
WHERE source_system LIKE 'EC%%' AND mgclear_time <> '' AND SUBSTR(mgclear_time, 1, 10) >= '2022-01-01' AND SUBSTR(mgclear_time, 1, 10) < '2025-07-01'
GROUP BY store_code, brand_code, system_order_no
) GROUP BY store_code, brand_code
ORDER BY brand_code, store_code;

