-- 1自定义销售本地表(订单相关)
CREATE TABLE audit_bi_pro.custom_online_sale_order_tmp_local
(
    `source_system` String COMMENT '来源系统',
    `order_date` String COMMENT '下单日期',
    `brand_code` String COMMENT '品牌编码',
    `order_time` String COMMENT '下单时间',
    `system_order_no` String COMMENT '系统订单号',
    `store_code` String COMMENT '店铺编码',
    `store_name` String COMMENT '店铺名称',
    `manual_order` String COMMENT '手工单',
    `is_swap_order` String COMMENT '是否换货单',
    `source_platform` String COMMENT '来源平台',
    `platform_order_no` String COMMENT '平台订单号',
    `nickname` String COMMENT '昵称',
    `consignee_name` String COMMENT '收货人名称',
    `consignee_tel` String COMMENT '收货人电话',
    `consignee_add` String COMMENT '收货人地址',
    `province` String COMMENT '省',
    `city` String COMMENT '市',
    `region` String COMMENT '区',
    `pay_time` String COMMENT '付款时间',
    `deliver_time` String COMMENT '发货时间',
    `mgclear_time` String COMMENT '钱货两清时间',
    `main_logistic_bill` String COMMENT '主物流号',
    `carrier` String COMMENT '承运商',
    `order_retail_amt` Decimal(18, 2) COMMENT '订单零售金额',
    `order_settle_amt` Decimal(18, 2) COMMENT '订单结算金额',
    `order_freight_amt` Decimal(18, 2) COMMENT '订单运费金额',
    `goods_barcode` String COMMENT '商品条码',
    `goods_qty` Int64 COMMENT '商品数量',
    `goods_price` Decimal(18, 2) COMMENT '商品单价',
    `goods_amt` Decimal(18, 2) COMMENT '订单商品均摊金额',
    `is_gift` String COMMENT '是否赠品'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/audit_bi_pro/custom_online_sale_order_tmp_local/{shard}',
 '{replica}')
PARTITION BY toYYYYMM(toDate(order_date))
ORDER BY system_order_no
SETTINGS index_granularity = 8192
COMMENT '自定义电商销售-临时';

-- 1-2自定义销售本地表(订单相关)
CREATE TABLE audit_bi_pro.custom_online_sale_order_local
(
    `source_system` String COMMENT '来源系统',
    `order_date` String COMMENT '下单日期',
    `brand_code` String COMMENT '品牌编码',
    `order_time` String COMMENT '下单时间',
    `system_order_no` String COMMENT '系统订单号',
    `store_code` String COMMENT '店铺编码',
    `store_name` String COMMENT '店铺名称',
    `manual_order` String COMMENT '手工单',
    `is_swap_order` String COMMENT '是否换货单',
    `source_platform` String COMMENT '来源平台',
    `platform_order_no` String COMMENT '平台订单号',
    `nickname` String COMMENT '昵称',
    `consignee_name` String COMMENT '收货人名称',
    `consignee_tel` String COMMENT '收货人电话',
    `consignee_add` String COMMENT '收货人地址',
    `province` String COMMENT '省',
    `city` String COMMENT '市',
    `region` String COMMENT '区',
    `pay_time` String COMMENT '付款时间',
    `deliver_time` String COMMENT '发货时间',
    `mgclear_time` String COMMENT '钱货两清时间',
    `main_logistic_bill` String COMMENT '主物流号',
    `carrier` String COMMENT '承运商',
    `order_retail_amt` Decimal(18, 2) COMMENT '订单零售金额',
    `order_settle_amt` Decimal(18, 2) COMMENT '订单结算金额',
    `order_freight_amt` Decimal(18, 2) COMMENT '订单运费金额',
    `goods_barcode` String COMMENT '商品条码',
    `goods_qty` Int64 COMMENT '商品数量',
    `goods_price` Decimal(18, 2) COMMENT '商品单价',
    `goods_amt` Decimal(18, 2) COMMENT '订单商品均摊金额',
    `is_gift` String COMMENT '是否赠品',
    `min_order_time` String COMMENT '每个平台订单最小下单时间'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/audit_bi_pro/custom_online_sale_order_local/{shard}',
 '{replica}')
PARTITION BY toYYYYMM(toDate(order_date))
ORDER BY system_order_no
SETTINGS index_granularity = 8192
COMMENT '自定义电商销售';

-- 2自定义销售本地表(账单相关)
CREATE TABLE audit_bi_pro.custom_online_sale_bill_local
(
    `source_system` String COMMENT '来源系统',
    `order_date` String COMMENT '下单日期',
    `brand_code` String COMMENT '品牌编码',
    `order_time` String COMMENT '下单时间',
    `system_order_no` String COMMENT '系统订单号',
    `store_code` String COMMENT '店铺编码',
    `store_name` String COMMENT '店铺名称',
    `manual_order` String COMMENT '手工单',
    `is_swap_order` String COMMENT '是否换货单',
    `source_platform` String COMMENT '来源平台',
    `platform_order_no` String COMMENT '平台订单号',
    `nickname` String COMMENT '昵称',
    `consignee_name` String COMMENT '收货人名称',
    `consignee_tel` String COMMENT '收货人电话',
    `consignee_add` String COMMENT '收货人地址',
    `province` String COMMENT '省',
    `city` String COMMENT '市',
    `region` String COMMENT '区',
    `pay_time` String COMMENT '付款时间',
    `deliver_time` String COMMENT '发货时间',
    `mgclear_time` String COMMENT '钱货两清时间',
    `main_logistic_bill` String COMMENT '主物流号',
    `carrier` String COMMENT '承运商',
    `order_retail_amt` Decimal(18, 2) COMMENT '订单零售金额',
    `order_settle_amt` Decimal(18, 2) COMMENT '订单结算金额',
    `order_freight_amt` Decimal(18, 2) COMMENT '订单运费金额',
    `goods_barcode` String COMMENT '商品条码',
    `goods_qty` Int64 COMMENT '商品数量',
    `goods_price` Decimal(18, 2) COMMENT '商品单价',
    `goods_amt` Decimal(18, 2) COMMENT '订单商品均摊金额',
    `is_gift` String COMMENT '是否赠品'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/audit_bi_pro/custom_online_sale_bill_local/{shard}',
 '{replica}')
PARTITION BY toYYYYMM(toDate(order_date))
ORDER BY system_order_no
SETTINGS index_granularity = 8192
COMMENT '自定义电商销售';

-- 
CREATE TABLE `audit_bi_pro`.`custom_online_sale_order` on cluster cluster2
as `audit_bi_pro`.`custom_online_sale_order_local`
ENGINE = Distributed(cluster2,audit_bi_pro,custom_online_sale_order_local,hiveHash(system_order_no))
COMMENT '自定义电商销售';

CREATE TABLE `audit_bi_pro`.`custom_online_sale_bill` on cluster cluster2
as `audit_bi_pro`.`custom_online_sale_bill_local`
ENGINE = Distributed(cluster2,audit_bi_pro,custom_online_sale_bill_local,hiveHash(system_order_no))
COMMENT '自定义电商销售';

--DROP TABLE audit_bi_pro.custom_online_sale_bill ON cluster cluster2;
-- 验证
SELECT COUNT() FROM custom_online_sale_order_local;
SELECT COUNT() FROM custom_online_sale_order;
SELECT COUNT() FROM custom_online_sale_bill_local;
SELECT COUNT() FROM custom_online_sale_bill;

-- 3
TRUNCATE TABLE custom_online_sale_order_local;
TRUNCATE TABLE custom_online_sale_order_tmp_local;
-- 4
TRUNCATE TABLE custom_online_sale_bill_local;

-- 5
INSERT INTO custom_online_sale_order_tmp_local
SELECT
	source_system,
	order_date,
	brand_code,
	order_time,
	system_order_no,
	store_code,
	store_name,
	manual_order,
	is_swap_order,
	source_platform,
    platform_order_no_new AS platform_order_no,
    nickname,
	consignee_name,
	consignee_tel,
	consignee_add,
	province,
	city,
	region,
	pay_time,
	deliver_time,
	mgclear_time,
	main_logistic_bill,
	carrier,
    
    toDecimal64(if(i < k, order_retail_amt_quotient, order_retail_amt_quotient + order_retail_amt_remainder), 2) / 100 AS order_retail_amt,
    toDecimal64(if(i < k, order_settle_amt_quotient, order_settle_amt_quotient + order_settle_amt_remainder), 2) / 100 AS order_settle_amt,
    toDecimal64(if(i < k, order_freight_amt_quotient, order_freight_amt_quotient + order_freight_amt_remainder), 2) / 100 AS order_freight_amt,
    goods_barcode,
    if(i > (k - goods_qty_remainder), goods_qty_div + 1, goods_qty_div) AS t,
    goods_price,
    toDecimal64(if(i < k, goods_amt_quotient, goods_amt_quotient + goods_amt_remainder), 2) / 100 AS goods_amt,
    is_gift
FROM (
    SELECT 
        splitByChar(',', platform_order_no) AS platform_order_no_array,
        length(platform_order_no_array) AS k,
    	toInt64(k) AS k_dec,
        
        source_system,
		order_date,
		brand_code,
		order_time,
		system_order_no,
		store_code,
		store_name,
		manual_order,
		is_swap_order,
		source_platform,
		nickname,
		consignee_name,
		consignee_tel,
		consignee_add,
		province,
		city,
		region,
		pay_time,
		deliver_time,
		mgclear_time,
		main_logistic_bill,
		carrier,
		
        order_retail_amt * 100 AS order_retail_amt_cents,
        (toInt64(order_retail_amt_cents) div k_dec) AS order_retail_amt_quotient,
        (toInt64(order_retail_amt_cents) % k_dec) AS order_retail_amt_remainder,
		
        order_settle_amt * 100 AS order_settle_amt_cents,
        (toInt64(order_settle_amt_cents) div k_dec) AS order_settle_amt_quotient,
        (toInt64(order_settle_amt_cents) % k_dec) AS order_settle_amt_remainder,
		
        order_freight_amt * 100 AS order_freight_amt_cents,
        (toInt64(order_freight_amt_cents) div k_dec) AS order_freight_amt_quotient,
        (toInt64(order_freight_amt_cents) % k_dec) AS order_freight_amt_remainder,
		
		goods_barcode,
		
        (goods_qty div k_dec) AS goods_qty_div,
        (goods_qty % k_dec) AS goods_qty_remainder,
        
		goods_price,
		
        goods_amt * 100 AS goods_amt_cents,
        (toInt64(goods_amt_cents) div k_dec) AS goods_amt_quotient,
        (toInt64(goods_amt_cents) % k_dec) AS goods_amt_remainder,
		
		is_gift
    FROM dwd_trade_hkaudit_ecommerce_sale_mt
    WHERE source_system IN ('EC_HIS_NEW', 'EC_HIS2', 'E3PLUS_NEW2', 'JXHE3', 'SBZ_HIS')
		OR (source_system = 'SBZE3' AND SUBSTR(system_order_no, 1, 4) <> 'XSDD' AND SUBSTR(system_order_no, 1, 5) <> 'XSJSD')
)
ARRAY JOIN platform_order_no_array AS platform_order_no_new, arrayEnumerate(platform_order_no_array) AS i; 

-- 6
INSERT INTO custom_online_sale_bill_local
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system IN ('PF_OCP_NEW', 'PF_ECP_NEW2', 'PF_YEP_NEW', 'PF_ESP_NEW')
	OR (source_system = 'SBZE3' AND (SUBSTR(system_order_no, 1, 4) = 'XSDD' OR SUBSTR(system_order_no, 1, 5) = 'XSJSD'));

-- 7处理手工单时间问题
UPDATE custom_online_sale_order_tmp_local SET
	order_time = deliver_time
WHERE source_system IN ('E3PLUS_NEW2', 'EC_HIS_NEW') AND manual_order = '是' AND deliver_time < order_time ;

-- 7-2处理城市问题
-- 省直辖县级行政区 -> 区域替换城市（其他区和空也直接换） 
-- 湖北省直辖县 -> 区域替换城市（其它区也直接换） 
-- 县 -> 省份替换城市
-- 自治区直辖县级行政区划 -> 区域替换城市
-- 省直辖县级行政区划 -> 区域替换城市（其他区和空也直接换） 
-- 新疆维吾尔自治区直辖县 -> 区域替换城市
-- 维吾尔自治区 -> 区域替换城市
-- 河南省直辖县 -> 区域替换城市
-- 市辖区 -> 省份替换城市
-- 广东 -> 区域替换城市
-- 湖北 -> 区域替换城市
UPDATE custom_online_sale_order_tmp_local SET
	city = region
WHERE city IN ('省直辖县级行政区', '湖北省直辖县', '自治区直辖县级行政区划', '省直辖县级行政区划', '新疆维吾尔自治区直辖县', '维吾尔自治区', '河南省直辖县', '广东', '湖北');
UPDATE custom_online_sale_order_tmp_local SET
	city = province 
WHERE city IN ('县', '市辖区');

-- 7-3非EC的订单运费改为0
UPDATE custom_online_sale_order_tmp_local SET
	order_freight_amt = 0 
WHERE source_system NOT IN ('EC_HIS_NEW', 'EC_HIS2');

-- 7-4更新每个订单的时间为最小
INSERT INTO custom_online_sale_order_local
SELECT t1.*, CASE WHEN t1.platform_order_no = '' THEN t1.order_time ELSE t2.order_time_t END
FROM custom_online_sale_order_tmp_local t1 LEFT JOIN (
	SELECT
		store_code,
		platform_order_no,
		MIN(order_time) AS order_time_t
	FROM custom_online_sale_order_tmp_local
	GROUP BY store_code, platform_order_no
) t2 ON t1.store_code = t2.store_code AND t1.platform_order_no = t2.platform_order_no
WHERE t1.order_time < '2023-10-01 00:00:00';

INSERT INTO custom_online_sale_order_local
SELECT t1.*, CASE WHEN t1.platform_order_no = '' THEN t1.order_time ELSE t2.order_time_t END
FROM custom_online_sale_order_tmp_local t1 LEFT JOIN (
	SELECT
		store_code,
		platform_order_no,
		MIN(order_time) AS order_time_t
	FROM custom_online_sale_order_tmp_local
	GROUP BY store_code, platform_order_no
) t2 ON t1.store_code = t2.store_code AND t1.platform_order_no = t2.platform_order_no
WHERE t1.order_time >= '2023-10-01 00:00:00' AND t1.order_time < '2025-01-01 00:00:00';

INSERT INTO custom_online_sale_order_local
SELECT t1.*, CASE WHEN t1.platform_order_no = '' THEN t1.order_time ELSE t2.order_time_t END
FROM custom_online_sale_order_tmp_local t1 LEFT JOIN (
	SELECT
		store_code,
		platform_order_no,
		MIN(order_time) AS order_time_t
	FROM custom_online_sale_order_tmp_local
	GROUP BY store_code, platform_order_no
) t2 ON t1.store_code = t2.store_code AND t1.platform_order_no = t2.platform_order_no
WHERE t1.order_time >= '2025-01-01 00:00:00';

-- 验证
SELECT * FROM custom_online_sale_order_local WHERE platform_order_no = ''; -- 1 -1

SELECT * FROM custom_online_sale_order_local WHERE platform_order_no IN (
SELECT platform_order_no FROM custom_online_sale_order_local GROUP BY platform_order_no HAVING COUNT(DISTINCT order_time) > 1) ORDER BY platform_order_no;

-- 验证
SELECT source_system , system_order_no , platform_order_no , order_time , deliver_time , pay_time , mgclear_time, dateDiff('day', toDateTime(deliver_time), toDateTime(order_date))
FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm WHERE source_system IN ('E3PLUS_NEW2', 'EC_HIS_NEW') AND manual_order = '是' AND deliver_time < order_time
order by dateDiff('day', toDateTime(deliver_time), toDateTime(order_date)) desc;
SELECT source_system, manual_order , COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm WHERE mgclear_time  <> '' AND mgclear_time < order_time GROUP BY source_system, manual_order ;
-- 
SELECT source_system, COUNT(DISTINCT system_order_no), SUM(goods_amt) FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE store_code = 'DHF3' AND SUBSTR(order_time, 1, 4) = '2024' GROUP BY source_system ;
SELECT source_system, COUNT(DISTINCT system_order_no), SUM(goods_amt) FROM custom_online_sale_order_local dthesm  WHERE store_code = 'DHF3' AND SUBSTR(order_time, 1, 4) = '2024' GROUP BY source_system ;

-- 验证数量
SELECT * FROM custom_online_sale_order_local WHERE goods_amt  < 0;
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE platform_order_no LIKE '%2355572484400219372%';

SELECT  * 
FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no LIKE '%288800263395%';
SELECT  * 
FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no = '284801595189';
SELECT  * 
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no LIKE '%288800263395%';
SELECT  * 
FROM dwd_trade_hkaudit_ecommerce_sale_change_mt WHERE platform_order_no = '284801595189';

SELECT COUNT(DISTINCT platform_order_no) FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZ_HIS' AND goods_amt < 0;
SELECT COUNT(DISTINCT platform_order_no) FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZ_HIS' AND goods_amt < 0 AND SUBSTR(order_time, 1, 10) >= '2024-04-01';
SELECT COUNT(DISTINCT platform_order_no) FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZ_HIS' AND SUBSTR(order_time, 1, 10) >= '2024-04-01';
SELECT DISTINCT source_system FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE goods_amt < 0;

SELECT * FROM  custom_online_sale_order_local  WHERE platform_order_no = '';
SELECT * FROM  dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no = '' AND source_system = 'SBZE3';
SELECT DISTINCT source_system  FROM  custom_online_sale_return_local  WHERE platform_order_no = '';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt dthesm  WHERE system_order_no = 'BS5324000002102';

SELECT MIN(order_time), MAX(order_time) FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZ_HIS';

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

SELECT COUNT()  FROM custom_online_sale_order_local WHERE source_system = 'SBZ_HIS' AND is_swap_order  = '是';


SELECT * FROM custom_online_sale_order_local WHERE source_system = 'EC_HIS_NEW' AND order_freight_amt > 0 ORDER BY platform_order_no ;


SELECT COUNT(DISTINCT system_order_no) FROM custom_online_sale_order_local; -- 99353755
SELECT COUNT(DISTINCT system_order_no) FROM custom_online_sale_bill_local; -- 61136
SELECT COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_mt; -- 99414891

-- 8这个情况的账单在退货表，提取到销售表，和其他来源系统统一 3765512
INSERT INTO custom_online_sale_bill_local
SELECT 
	source_system,
	create_date,
	brand_code,
	create_time,
	system_order_no,
	store_code,
	store_name,
	'',
	'否',
	source_platform,
	platform_order_no,
	nickname,
	consignee_name,
	consignee_tel,
	consignee_add,
	province,
	city,
	region,
	'',
	'',
	mgclear_time,
	return_logistic_bill,
	return_carrier,
	0,
	0,
	return_freight_amt,
	return_goods_barcode,
	return_goods_qty,
	0,
	return_goods_amt,
	is_gift
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE source_system = 'SBZE3' AND SUBSTR(system_order_no, 1, 5) = 'XSJSD';

-- 数据验证
SELECT COUNT(platform_order_no) FROM custom_online_sale_local;
SELECT SUM(num) FROM (
SELECT LENGTH(platform_order_no) - LENGTH(REPLACE(platform_order_no, ',', '')) + 1 AS num FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_code IN ('DAA0','DAB7','DX01','DPL1','DPL2','DPH2','DPH1','DPH3','DPH4','DPH5','DPH6','DPHD','DPHH','DPHL','DPHP','DPHN','DPHM','DPHR','DPHS','DPHQ','DPHT','DPHU','DPHV','DPHW','DPHX','DPHY','DPH0','DPA1','DPA2','DPA3','DPA4','DPA5','DPA6','DCA0','DCA9','DCD5','DCA7','DCB1','DCC7','DCC3','DCC5','DCE7','DCB7','DCD2','DCB0','DCD8','DCA5','DCF3','DCF4','DCF2','DCF5','DCF6','DCF8','DCF9','DCF1','DCG1','DCG3','DCG4','DCG5','DCH1','DCG6','DCG7','DCC1','DCG8','DCG9','DCH2','DCH3','DCH4','DCH5','DCH6','DCH7','DCH8','DCH9','DCL1','DCL2','DCL3','DCL4','DCL5','DCL6','DCL8','DCJ1','DCL9','DCJ2','DCJ5','DCJ3','DCJ4','DCJ6','DCJ7','DCJ9','DCK1','DCK2','DCK4','DCK5','DCK3','DCK6','DCK7','DCK8','DCK9','DCM2','DCM7','DCM1','DCM3','DCM4','DCM5','DCN2','DCN3','DCM9','DCM6','DCN1','DCN4','DCN5','DCN6','DCP4','DCN8','DCN9','DCP1','DCP7','DCP2','DCP5','DCP6','DCP8','DCP9','DCQ2','DCQ3','DCQ4','DCQ5','DCP3','DCQ8','DCQ6','DCQ7','DCQ9','DCR1','DCR3','DCR2','DCR4','DCR5','DC01','DC02','DC04','DC05','DC06','DC07','DC08','DC09','DC0A','DC0B','DC0C','DC0D','DC0E','DC0F','DC0G','DC0H','DC0J','DC0K','DC0L','DC0M','DC0N','DC0P','DC0Q','DC0R','DC0S','DC0T','DC0V','DC0U','DCN7','DC0X','DC0W','DC0Y','DC0Z','DC10','DC11','DC13','DC12','DC14','DC15','DC16','DC1A','DC17','DC1B','DC1C','DC1E','DC1D','DC1F','DC1G','DC1Q','DC1H','DC1J','DC1K','DC1L','DC1M','DC1N','DC1P','DC1R','DC1T','DFA5','DFB0','DFB5','DFC8','DFG4','DFC0','DFG2','DFG7','DFG3','DFG1','DFB3','DFF9','DFF3','DFB9','DFG8','DFH2','DFH1','DFH3','DFH4','DFH6','DFJ1','DFJ2','DFJ3','DFJ4','DFJ5','DFJ9','DFJ7','DFK1','DFJ6','DFJ8','DFK3','DF04','DFK2','DF05','DF07','DF0G','DF0E','DF0H','DF0K','DF0M','DF0D','DF06','DF0Q','DF0P','DF0N','DF0R','DF0F','DF0W','DF0T','DF0V','DF0J','DF0X','DF12','DK05','DK03','DK01','DK02','DK06','DK04','DK09','DK08','DK0B','DK0C','DK0A','DK0F','DK0D','DK0E','DK0G','DK0K','DK0L','DK0H','DGA0','DGA3','DGA5','DGA2','DGB4','DGA4','DGB1','DGA8','DGC9','DGD1','DGD2','DGD4','DGC4','DGC2','DGD5','DGD6','DGC6','DGB9','DGC7','DGD7','DGD8','DGD9','DGE1','DGD3','DGE2','DGE6','DGE5','DGE7','DGF1','DGE8','DGE9','DGF2','DGF3','DGF4','DGF5','DGF6','DGF8','DGF7','DGF9','DGG1','DGG2','DGG3','DGG4','DGG5','DGG6','DGG8','DGG7','DGH1','DGH2','DGH8','DGH9','DGH4','DGH3','DGH5','DGH6','DGH7','DGJ5','DGJ2','DGJ3','DGJ4','DGJ7','DGJ8','DGJ9','DGJ6','DGK2','DGK3','DGK8','DGK9','DGK4','DGK1','DGK5','DGK6','DGL2','DGL3','DGK7','DGL1','DGL5','DGJ1','DGL8','DGL9','DGL4','DGM2','DGL6','DGL7','DGN2','DGM3','DGM4','DGM5','DGM1','DGM8','DG01','DGM9','DGM6','DGN1','DGM7','DG05','DG02','DG04','DG0A','DG03','DGN3','DG06','DG08','DG0C','DG09','DG0B','DG07','DG0E','DG0F','DG0G','DG0Q','DG0N','DG0H','DG0J','DG0K','DG0L','DG0U','DG0V','DG0M','DG0P','DG0R','DG0S','DG10','DG11','DG0T','DG0W','DG13','DG0X','DG0Y','DG14','DG15','DG0Z','DG12','DG17','DG18','DG1A','DG1B','DG19','DG16','DG1D','DG1E','DG1G','DG1H','DG1F','DG1C','DG1K','DG1L','DG1R','DG1S','DG1M','DG1J','DG1U','DG1Q','DG1T','DG1V','DG1W','DG1N','DG1P','DG1X','DG1Y','DG23','DG1Z','DG25','DG20','DG22','DG26','DG21','DG24','DG27','DG29','DG2A','DG2F','DG2G','DG2B','DG28','DG2C','DG2D','DG2J','DG2K','DG2E','DG2H','DG2M','DG2N','DG2Q','DG2R','DG2P','DG2L','DG2T','DG2U','DG2Z','DG2V','DG2S','DG2W','DG2X','DG35','DG36','DG2Y','DG31','DG32','DG33','DG3B','DG3C','DG34','DG37','DH1D','DH3U','DH4H','DH4U','DH5K','DH5R','DH5U','DH5V','DH5Z','DH6R','DH6V','DH6Z','DH7R','DH7V','DH7Z','DHJ7','H5LX','DH1A','DH1C','DH1E','DH1G','DH1H','DH1J','DH1K','DH1M','DH1P','DH1Z','DH2C','DH2F','DH2G','DH2H','DH2J','DH2K','DH2M','DH2N','DH2R','DH2S','DH2V','DH3A','DH3D','DH3F','DH3G','DH3J','DH3R','DH3V','DH3Y','DH3Z','DH4C','DH4F','DH4J','DH4N','DH4V','DH4X','DH4Y','DH5A','DH5C','DH5F','DH5J','DH5N','DH5P','DH5W','DH5X','DH5Y','DH6A','DH6F','DH6H','DH6J','DH6S','DH7H','DH7J','DH7L','DH7P','DH7S','DH7W','DH7Y','DH8A','DH8D','DH8H','DH8L','DH8N','DH8P','DH8Q','DH8S','DH8V','DH8W','DH8Y','DH8Z','DH9A','DH9B','DH9H','DH9J','DH9N','DH9P','DH9Q','DH9S','DH9U','DH9V','DH9Z','DHA0','DHA1','DHA2','DHA3','DHA4','DHA5','DHA6','DHAB','DHAK','DHB1','DHB2','DHB3','DHB4','DHB5','DHB6','DHB8','DHC7','DHD9','DHE5','DHE6','DHE8','DHE9','DHF0','DHF2','DHF3','DHF6','DHF9','DHG6','DHG7','DHG8','DHG9','DHH0','DHH1','DHH6','DHH7','DHH8','DHH9','DHI1','DHI3','DHI4','DHI5','DHI6','DHI7','DHI8','DHJ1','DHJ5','DHJ6','DHK2','DHK8','DHK9','DHL0','DHL1','DHL2','DHL3','DHM5','DHM6','DHN2','DHN5','DHP9','DHQ8','DHR1','DHR9','DHS7','DHS9','DHT2','DHT3','DHT9','DHU1','DHU6','DHU7','DHU8','DHU9','DHW2','DHW3','DHW4','DHW5','DHW6','DHW7','DHW8','DHW9','DHX1','DHX3','DHX4','DHX5','DHY1','DHY2','DHY3','DHY4','DHY5','DHY6','DHZ2','DHZ3','DHZ4','DHZ6','DHZ7','DHZ8','DH8M','DH9M','DHAA','DHR6','DH1R','DH2E','DH3M','DH4S','DH6M','DH6X','DH8B','DH9D','DH9R','DH9W','DHAF','DHAG','DHAH','DHAI','DHAJ','DHAL','DHE4','DHI9','DHM7','DHAO','DHAP','DHAQ','DHN7','DHP2','DH1L','DH2X','DH3C','DH3N','DH6N','DH6Y','DH7N','DHAC','DHAM','DHAN','DHAU','DHAV','DHAW','DHAX','DHAY','DHBE','DHBF','DHBR','DHY9','DHBZ','DHCA','DHCB','DH2P','DH3P','DH4D','DH4Z','DHBG','DHBH','DHBJ','DHBT','DHBW','DHBX','DHC0','DHCG','DHCJ','DHCR','DHDW','DHE0','DHN6','DHP5','DHU2','DHDT','DHDU','DHDV','DH1V','DH5E','DH6E','DH7M','DH7X','DHBA','DHBB','DHCK','DHCL','DHCM','DHCN','DHCP','DHEE','DHEF','DHEA','DHEB','DHEC','DH1N','DH2L','DH4G','DHDY','DHEH','DHEQ','DHEV','DH2D','DHER','DHES','DHET','DH1W','DH4P','DH5S','DH6Q','DHBC','DHBV','DHBY','DHCF','DHEW','DHEX','DHEY','DHFH','DHFJ','DHFK','DH1S','DHAT','DHAZ','DHEK','DHEZ','DHFA','DHFB','DHFC','DHFD','DHFF','DHFL','DHFR','DHFS','DHFT','DHFY','DHFZ','DHGC','DHGD','DHGH','DHGJ','DHGK','DHGS','DHGW','DH1X','DHFP','DHGL','DHGT','DHGZ','DHHA','DHHB','DHHC','DHHE','DHN4','DHGV','DHGX','DHHM','DH2T','DHGF','DHGY','DHHH','DHHJ','DHHK','DHHQ','DHHR','DHHS','DHHT','DHHL','DHHN','DHJK','DHFM','DHHF','DHJE','DHJF','DHJG','DHJL','DHJQ','DHJH','DHJJ','DHCD','DHGE','DHJR','DHJV','DHKB','DHJW','DHJX','DHJY','DHKG','DHKX','DHKD','DHKK','DHKY','DHKT','DHKV','DHKW','DHLM','DH3L','DH7B','DHBD','DHFG','DHKL','DHKN','DHKP','DHKQ','DHKS','DHLB','DHLE','DHX9','DHLJ','DHLK','DHLL','DHNG','DHCC','DHGA','DHJS','DHJZ','DHLA','DHLG','DHLH','DHLT','DHLY','DHLZ','DHNB','DHNJ','DHNK','DHND','DHNE','DHNF','DHNV','DHLF','DHLU','DHMA','DHMW','DHMY','DHNP','DHNY','DHPB','DHPD','DHPK','DHPP','DHNR','DHNS','DHNT','DHRG','DHLN','DHLW','DHMV','DHNL','DHNW','DHNX','DHPE','DHPM','DHPS','DHPZ','DHQB','DHQP','DHQR','DHRC','DHRH','DHRJ','DHRK','DHRL','DHRM','DHRN','DHRD','DHRE','DHRF','DHRW','DHFV','DHNM','DHQD','DHSD','DHRT','DHRU','DHRV','DHSC','DHSN','DHSP','DHMK','DHMM','DHNZ','DHPA','DHRS','DHRX','DHRZ','DHSA','DHSG','DHSH','DHSJ','DHSQ','DHSS','DHST','DHSU','DHSV','DHSW','DHSX','DHSY','DHTA','DHTB','DHTC','DHTD','DHTE','DHTL','DHTM','DHSK','DHSL','DHSM','DHTJ','DHTK','DHTR','DHPV','DHQH','DHQY','DHRA','DHSE','DHSZ','DHTN','DHTP','DHTQ','DHTS','DHVK','DHTF','DHTG','DHTH','DHVV','DHVW','DHMC','DHNN','DHNQ','DHQF','DHQT','DHQW','DHSF','DHTW','DHVY','DHVZ','DHY8','DHUD','DHUE','DHVS','DHVT','DHVU','DHBN','DHKF','DHLS','DHMX','DHQJ','DHUF','DHUG','DHUH','DHUU','DHVB','DHVD','DHVX','DHUA','DHUB','DHUC','DHUW','DHUY','DHUZ','DHAD','DHHG','DHLV','DHPX','DHWC','DHWE','DHWF','DHWG','DHUV','DHUX','DHWR','DH03','DHQE','DHQM','DHUT','DHVE','DHWD','DHWL','DHWM','DHWN','DHWP','DHWQ','DHWS','DH06','DH07','DH0C','DH0H','DH0N','DHP8','DHTV','DHTZ','DH0U','DH08','DH09','DH0A','DH0B','DH0J','DH0P','DH0Z','DH10','DH11','DH12','DH13','DH14','DH15','DHQN','DHQV','DH0S','DH0T','DH0W','DH49','DH04','DH0M','DH16','DH51','DH52','DH53','DH54','DH56','DHRP','DHVC','DH46','DH47','DH48','DH62','DH0D','DH0E','DH0F','DH0R','DH0X','DH25','DH35','DH57','DH58','DH7C','DHQZ','DHVA','DH59','DH60','DH61','DH85','DH87','DH20','DH21','DH64','DH74','DH75','DH76','DH77','DH78','DH81','DH82','DH92','DH93','DH94','DH95','DH96','DHVP','DH84','DH86','DH91','DHFU','DHH2','DHHU','DH0G','DH55','DHJU','DHP0','DHPF','DHPU','DHQ0','DHQU','DHR0','DHTU','DHHD','DHHX','DHWW','DH0Y','DH9X','DHK0','DHKU','DHLC','DHLD','DHLP','DHM0','DHS0','DHT0','DHXA','DHXB','DHXC','DHXD','DHXE','DHGU','DHWU','DHWV','DHXJ','DH17','DH18','DHX0','DHXL','DHXM','DHXN','DHXP','DHXQ','DHXR','DHXS','DHXT','DHXU','DHXG','DHXH','DHXK','DHXW','DHXY','DH19','DH37','DH39','DHN0','DHNU','DHY0','DHYC','DHYD','DHYG','DHYH','DHXX','DHXZ','DHYM','DHYP','DHRR','DHYJ','DHYL','DHYS','DH99','DHYN','DHYQ','DHYU','DHYW','DH0L','DH4R','DHMT','DHWZ','DHXV','DHYV','DHYX','DHZE','DHZG','DHPQ','DHRQ','DHZL','DHZF','DHZH','DHZV','DHZX','DHMU','DHQQ','DHTT','DHXF','DHZM','DHZN','DHZQ','DHZR','DHZS','DHZZ','VH04','VH05','DHZW','DHZY','VH09','VH0B','DHU0','DHVJ','DHWA','VH01','VH02','VH07','DH00','VH0A','VH0C','VH0L','VH0P','DH22','DH3S','DH4W','DHTX','DHUS','DHZT','VH03','VH0F','VH0G','VH0H','VH0K','VH0Q','VH0R','VH0W','VH0M','VH0N','VH14','VH16','DH4M','DHAE','DHPH','VH0T','VH0U','VH0V','VH0Z','VH10','VH1A','VH1D','VH1E','VH1K','VH15','VH17','VH1L','VH1N','VH1U','VH1M','VH1P','VH1V','VH1W','DH8K','DHBM','VH0J','VH0X','VH1Q','VH1S','VH1Y','DBE7','DBE8','DBE9','DBA0','DBA3','DBA9','DBB3','DBB4','DBB5','DBC0','DBE4','DBE5','DBF1','DBE1','DBF2','DBF3','DBF4','DBF5','DBF6','DBF7','DBF8','DBG1','DBG2','DBG3','DBG4','DBG5','DBG6','DBG7','DBG8','DBG9','DBH1','DBH2','DBH3','DBH4','DBH5','DBH6','DBH7','DBH8','DBH9','DBJ1','DBJ2','DBJ3','DBJ4','DBJ5','DBJ6','DBJ7','DBJ8','DBJ9','DBK1','DBK2','DBK3','DBK4','DBK5','DBK6','DBK7','DBK8','DBK9','DBL1','DBL2','DBL3','DBL4','DBL5','DBL6','DBL7','DBL8','DBL9','DBM1','DBM2','DB01','DB02','DB03','DB06','DB04','DB05','DB07','DB08','DB09','DB0A','DB0B','DB0C','DB0D','DB0E','DB0F','DB0G','DB0H','DB0J','DB0K','DB0L','DB0M','DB0N','DB0P','DB0Q','DB0R','DB0S','DB0T','DB0U','DB0V','DB0W','DB0X','DB0Y','DB0Z','DB10','DB11','DB12','DB13','DB14','DB15','DB16','DB17','DB18','DB19','DB1A','DB1B','DB1C','DB1D','DB1E','DB1F','DB1G','DB1H','DB1J','DB1K','DB1L','DB1M','DB1N'));

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no LIKE '%,%';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no = 'TM261600075604';
SELECT * FROM custom_online_sale_local WHERE system_order_no = 'TM261600075604';

SELECT *
FROM (SELECT system_order_no, SUM(goods_amt) AS amt1 FROM dwd_trade_hkaudit_ecommerce_sale_mt 
WHERE store_code = 'DHF3'
GROUP BY system_order_no) a LEFT JOIN (
SELECT system_order_no, SUM(goods_amt) AS amt2 FROM custom_online_sale_local 
WHERE store_code = 'DHF3'
GROUP BY system_order_no) b ON a.system_order_no = b.system_order_no
WHERE ABS(amt1 - amt2) > 0;

-- 9自定义退销本地表
CREATE TABLE audit_bi_pro.custom_online_sale_return_local
(
    `source_system` String COMMENT '来源系统',
    `create_date` String COMMENT '创建日期',
    `brand_code` String COMMENT '品牌编码',
    `create_time` String COMMENT '创建时间',
    `system_order_no` String COMMENT '系统订单号',
    `store_code` String COMMENT '店铺编码',
    `store_name` String COMMENT '店铺名称',
    `association_order_no` String COMMENT '关联系统订单号',
    `source_platform` String COMMENT '来源平台',
    `platform_order_no` String COMMENT '平台订单号',
    `nickname` String COMMENT '昵称',
    `consignee_name` String COMMENT '收货人名称',
    `consignee_tel` String COMMENT '收货人电话',
    `consignee_add` String COMMENT '收货人地址',
    `province` String COMMENT '省',
    `city` String COMMENT '市',
    `region` String COMMENT '区',
    `return_freight_amt` Decimal(18,2) COMMENT '退款运费金额',
    `return_carrier` String COMMENT '退款物流承运商',
    `return_logistic_bill` String COMMENT '退款物流单号',
    `return_reason` String COMMENT '平台退款原因',
    `mgclear_time` String COMMENT '钱货两清时间',
    `return_goods_code` String COMMENT '退货商品编码',
    `return_goods_name` String COMMENT '退货商品名称',
    `return_goods_barcode` String COMMENT '退货商品条码',
    `return_goods_qty` Int64 COMMENT '退货商品数量',
    `return_goods_amt` Decimal(18,2) COMMENT '退货单商品均摊退货金额',
    `is_gift` String COMMENT '是否赠品'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/audit_bi_pro/custom_online_sale_return_local/{shard}',
 '{replica}')
PARTITION BY toYYYYMM(toDate(create_date))
ORDER BY system_order_no
SETTINGS index_granularity = 8192
COMMENT '自定义电商退销';

-- 9自定义退销本地表
CREATE TABLE audit_bi_pro.custom_online_sale_return_tmp_local
(
    `source_system` String COMMENT '来源系统',
    `create_date` String COMMENT '创建日期',
    `brand_code` String COMMENT '品牌编码',
    `create_time` String COMMENT '创建时间',
    `system_order_no` String COMMENT '系统订单号',
    `store_code` String COMMENT '店铺编码',
    `store_name` String COMMENT '店铺名称',
    `association_order_no` String COMMENT '关联系统订单号',
    `source_platform` String COMMENT '来源平台',
    `platform_order_no` String COMMENT '平台订单号',
    `nickname` String COMMENT '昵称',
    `consignee_name` String COMMENT '收货人名称',
    `consignee_tel` String COMMENT '收货人电话',
    `consignee_add` String COMMENT '收货人地址',
    `province` String COMMENT '省',
    `city` String COMMENT '市',
    `region` String COMMENT '区',
    `return_freight_amt` Decimal(18,2) COMMENT '退款运费金额',
    `return_carrier` String COMMENT '退款物流承运商',
    `return_logistic_bill` String COMMENT '退款物流单号',
    `return_reason` String COMMENT '平台退款原因',
    `mgclear_time` String COMMENT '钱货两清时间',
    `return_goods_code` String COMMENT '退货商品编码',
    `return_goods_name` String COMMENT '退货商品名称',
    `return_goods_barcode` String COMMENT '退货商品条码',
    `return_goods_qty` Int64 COMMENT '退货商品数量',
    `return_goods_amt` Decimal(18,2) COMMENT '退货单商品均摊退货金额',
    `is_gift` String COMMENT '是否赠品'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/audit_bi_pro/custom_online_sale_return_tmp_local/{shard}',
 '{replica}')
PARTITION BY toYYYYMM(toDate(create_date))
ORDER BY system_order_no
SETTINGS index_granularity = 8192
COMMENT '自定义电商退销-临时';

-- 10
TRUNCATE TABLE custom_online_sale_return_local;
TRUNCATE TABLE custom_online_sale_return_tmp_local;

-- 10-2退款中单号为空的补单号进去，通过销售系统
INSERT INTO custom_online_sale_return_tmp_local
SELECT
	source_system,
	create_date,
	brand_code,
	create_time,
	system_order_no,
	store_code,
	store_name,
	association_order_no,
	source_platform,
	CASE 
		WHEN platform_order_no = '' AND platform_order_no_t <> '' THEN platform_order_no_t
	ELSE platform_order_no
	END,
	nickname,
	consignee_name,
	consignee_tel,
	consignee_add,
	province,
	city,
	region,
	return_freight_amt,
	return_carrier,
	return_logistic_bill,
	return_reason,
	mgclear_time,
	return_goods_code,
	return_goods_name,
	return_goods_barcode,
	return_goods_qty,
	return_goods_amt,
	is_gift
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt t1
LEFT JOIN (SELECT store_code, system_order_no, MAX(platform_order_no) AS platform_order_no_t
	FROM dwd_trade_hkaudit_ecommerce_sale_mt GROUP BY store_code, system_order_no
) t2 ON t1.store_code = t2.store_code AND t1.association_order_no = t2.system_order_no;

-- SBZE3 存在部分已经拆单的？1211
SELECT store_code, system_order_no, MAX(source_system) FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no <> '' GROUP BY store_code, system_order_no HAVING COUNT(DISTINCT platform_order_no) > 1;
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no <> '' AND store_code = 'SF00692' AND system_order_no = 'BS5513000011955';
SELECT * FROM custom_online_sale_order_local WHERE platform_order_no <> '' AND store_code = 'SF00692' AND system_order_no = 'BS5513000011955';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE association_order_no IN (
	SELECT system_order_no FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no <> '' GROUP BY store_code, system_order_no HAVING COUNT(DISTINCT platform_order_no) > 1
);

-- 11  23344611
INSERT INTO custom_online_sale_return_local
SELECT
	source_system,
	create_date,
	brand_code,
	create_time,
	system_order_no,
	store_code,
	store_name,
	association_order_no,
	source_platform,
	platform_order_no_new AS platform_order_no,
	nickname,
	consignee_name,
	consignee_tel,
	consignee_add,
	province,
	city,
	region,
    
    toDecimal64(if(i < k, return_freight_amt_quotient, return_freight_amt_quotient + return_freight_amt_remainder), 2) / 100 AS return_freight_amt,
    
    return_carrier,
	return_logistic_bill,
	return_reason,
	mgclear_time,
	return_goods_code,
	return_goods_name,
	return_goods_barcode,
	
    if(i > (k - return_goods_qty_remainder), return_goods_qty_div + 1, return_goods_qty_div) AS t,
    toDecimal64(if(i < k, return_goods_amt_quotient, return_goods_amt_quotient + return_goods_amt_remainder), 2) / 100 AS return_goods_amt,
    
    is_gift
FROM (
    SELECT 
        splitByChar(',', platform_order_no) AS platform_order_no_array,
        length(platform_order_no_array) AS k,
    	toInt64(k) AS k_dec,
        
        source_system,
		create_date,
		brand_code,
		create_time,
		system_order_no,
		store_code,
		store_name,
		association_order_no,
		source_platform,
		nickname,
		consignee_name,
		consignee_tel,
		consignee_add,
		province,
		city,
		region,
		
        return_freight_amt * 100 AS return_freight_amt_cents,
        (toInt64(return_freight_amt_cents) div k_dec) AS return_freight_amt_quotient,
        (toInt64(return_freight_amt_cents) % k_dec) AS return_freight_amt_remainder,
        
		return_carrier,
		return_logistic_bill,
		return_reason,
		mgclear_time,
		return_goods_code,
		return_goods_name,
		return_goods_barcode,
		
        (return_goods_qty div k_dec) AS return_goods_qty_div,
        (return_goods_qty % k_dec) AS return_goods_qty_remainder,
        
        return_goods_amt * 100 AS return_goods_amt_cents,
        (toInt64(return_goods_amt_cents) div k_dec) AS return_goods_amt_quotient,
        (toInt64(return_goods_amt_cents) % k_dec) AS return_goods_amt_remainder,
		
		is_gift
    FROM custom_online_sale_return_tmp_local
    WHERE NOT (source_system = 'SBZE3' AND SUBSTR(system_order_no, 1, 5) = 'XSJSD')
)
ARRAY JOIN platform_order_no_array AS platform_order_no_new, arrayEnumerate(platform_order_no_array) AS i; 

-- 11-2非EC的订单运费改为0
UPDATE custom_online_sale_return_local SET
	return_freight_amt = 0 
WHERE source_system NOT IN ('EC_HIS_NEW', 'EC_HIS2');

SELECT * FROM custom_online_sale_return_local WHERE return_freight_amt <> 0 AND source_system IN ('EC_HIS_NEW', 'EC_HIS2');

-- 销售order和bill中店铺和退款店铺一致性问题 DYE2
SELECT * FROM custom_online_sale_bill_local WHERE store_code IN (SELECT DISTINCT store_code FROM custom_online_sale_return_local); 
SELECT * FROM custom_online_sale_bill_local WHERE store_code IN (SELECT DISTINCT store_code FROM custom_online_sale_order_local); 

SELECT * FROM custom_online_sale_return_local WHERE store_code = 'SF00891';
SELECT * FROM custom_online_sale_order_local WHERE store_code = 'SF00891';

SELECT * FROM custom_online_sale_bill_local WHERE store_code = 'SF00891';


SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no LIKE 'JXMHDD%';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no LIKE 'XSJSD%' AND source_system LIKE 'SBZ%';

-- 验证数量 21872332
SELECT COUNT(DISTINCT system_order_no) FROM custom_online_sale_return_local;

--
SELECT * FROM custom_online_sale_return_local WHERE platform_order_no = ''; 
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no ='BS5101000010785';
SELECT * FROM custom_online_sale_order_local  WHERE platform_order_no = '';
SELECT source_system, COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no = '' AND source_system LIKE 'SBZ%' GROUP BY source_system; 
SELECT source_system, COUNT()  FROM custom_online_sale_return_local WHERE platform_order_no = '' AND source_system LIKE 'SBZ%' GROUP BY source_system; 

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no = '' AND source_system LIKE 'SBZ_HI%'; 
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE platform_order_no = '' AND source_system LIKE 'SBZ_HI%'; 
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no = '' AND source_system LIKE 'SBZE%'; 
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no = '' AND source_system LIKE 'SBZE%' 
	AND system_order_no IN (SELECT system_order_no  FROM dwd_trade_hkaudit_ecommerce_sale_mt);
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_return_mt WHERE platform_order_no = '' AND source_system LIKE 'SBZE%' 
	AND association_order_no IN (SELECT system_order_no  FROM dwd_trade_hkaudit_ecommerce_sale_mt);

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE source_system = 'SBZ_HIS';

SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE system_order_no = '11040230047404';



-- 218712500
SELECT COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_return_mt;

-- 12自定义发货调整本地表
CREATE TABLE audit_bi_pro.custom_online_sale_change_local
(
    `source_system` String COMMENT '来源系统',
    `create_date` String COMMENT '创建日期',
    `brand_code` String COMMENT '品牌编码',
    `create_time` String COMMENT '创建时间',
    `system_order_no` String COMMENT '系统订单号',
    `store_code` String COMMENT '店铺编码',
    `store_name` String COMMENT '店铺名称',
    `source_platform` String COMMENT '来源平台',
    `platform_order_no` String COMMENT '平台订单号',
    `mgclear_time` String COMMENT '钱货两清时间',
    `special_barcode` String COMMENT '特殊条码',
    `goods_qty` Int64 COMMENT '商品数量',
    `special_barcode_amt` Decimal(18, 2) COMMENT '特殊条码均摊金额'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/audit_bi_pro/custom_online_sale_change_local/{shard}',
 '{replica}')
PARTITION BY toYYYYMM(toDate(create_date))
ORDER BY system_order_no
SETTINGS index_granularity = 8192
COMMENT '自定义电商发货调整类型';

-- 13
TRUNCATE TABLE custom_online_sale_change_local;

--14
INSERT INTO custom_online_sale_change_local
SELECT
	source_system,
	create_date,
	brand_code,
	create_time,
	system_order_no,
	store_code,
	store_name,
	source_platform,
	platform_order_no_new AS platform_order_no,
	mgclear_time,
	special_barcode,
	
    if(i > (k - goods_qty_remainder), goods_qty_div + 1, goods_qty_div) AS t,
    toDecimal64(if(i < k, special_barcode_amt_quotient, special_barcode_amt_quotient + special_barcode_amt_remainder), 2) / 100 AS special_barcode_amt
FROM (
    SELECT 
        splitByChar(',', platform_order_no) AS platform_order_no_array,
        length(platform_order_no_array) AS k,
    	toInt64(k) AS k_dec,
        
        source_system,
		create_date,
		brand_code,
		create_time,
		system_order_no,
		store_code,
		store_name,
		source_platform,
		platform_order_no,
		mgclear_time,
		special_barcode,
		
        (goods_qty div k_dec) AS goods_qty_div,
        (goods_qty % k_dec) AS goods_qty_remainder,
        
        special_barcode_amt * 100 AS special_barcode_amt_cents,
        (toInt64(special_barcode_amt_cents) div k_dec) AS special_barcode_amt_quotient,
        (toInt64(special_barcode_amt_cents) % k_dec) AS special_barcode_amt_remainder
		
    FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
    WHERE store_code IN ('DAA0','DAB7','DX01','DPL1','DPL2','DPH2','DPH1','DPH3','DPH4','DPH5','DPH6','DPHD','DPHH','DPHL','DPHP','DPHN','DPHM','DPHR','DPHS','DPHQ','DPHT','DPHU','DPHV','DPHW','DPHX','DPHY','DPH0','DPA1','DPA2','DPA3','DPA4','DPA5','DPA6','DCA0','DCA9','DCD5','DCA7','DCB1','DCC7','DCC3','DCC5','DCE7','DCB7','DCD2','DCB0','DCD8','DCA5','DCF3','DCF4','DCF2','DCF5','DCF6','DCF8','DCF9','DCF1','DCG1','DCG3','DCG4','DCG5','DCH1','DCG6','DCG7','DCC1','DCG8','DCG9','DCH2','DCH3','DCH4','DCH5','DCH6','DCH7','DCH8','DCH9','DCL1','DCL2','DCL3','DCL4','DCL5','DCL6','DCL8','DCJ1','DCL9','DCJ2','DCJ5','DCJ3','DCJ4','DCJ6','DCJ7','DCJ9','DCK1','DCK2','DCK4','DCK5','DCK3','DCK6','DCK7','DCK8','DCK9','DCM2','DCM7','DCM1','DCM3','DCM4','DCM5','DCN2','DCN3','DCM9','DCM6','DCN1','DCN4','DCN5','DCN6','DCP4','DCN8','DCN9','DCP1','DCP7','DCP2','DCP5','DCP6','DCP8','DCP9','DCQ2','DCQ3','DCQ4','DCQ5','DCP3','DCQ8','DCQ6','DCQ7','DCQ9','DCR1','DCR3','DCR2','DCR4','DCR5','DC01','DC02','DC04','DC05','DC06','DC07','DC08','DC09','DC0A','DC0B','DC0C','DC0D','DC0E','DC0F','DC0G','DC0H','DC0J','DC0K','DC0L','DC0M','DC0N','DC0P','DC0Q','DC0R','DC0S','DC0T','DC0V','DC0U','DCN7','DC0X','DC0W','DC0Y','DC0Z','DC10','DC11','DC13','DC12','DC14','DC15','DC16','DC1A','DC17','DC1B','DC1C','DC1E','DC1D','DC1F','DC1G','DC1Q','DC1H','DC1J','DC1K','DC1L','DC1M','DC1N','DC1P','DC1R','DC1T','DFA5','DFB0','DFB5','DFC8','DFG4','DFC0','DFG2','DFG7','DFG3','DFG1','DFB3','DFF9','DFF3','DFB9','DFG8','DFH2','DFH1','DFH3','DFH4','DFH6','DFJ1','DFJ2','DFJ3','DFJ4','DFJ5','DFJ9','DFJ7','DFK1','DFJ6','DFJ8','DFK3','DF04','DFK2','DF05','DF07','DF0G','DF0E','DF0H','DF0K','DF0M','DF0D','DF06','DF0Q','DF0P','DF0N','DF0R','DF0F','DF0W','DF0T','DF0V','DF0J','DF0X','DF12','DK05','DK03','DK01','DK02','DK06','DK04','DK09','DK08','DK0B','DK0C','DK0A','DK0F','DK0D','DK0E','DK0G','DK0K','DK0L','DK0H','DGA0','DGA3','DGA5','DGA2','DGB4','DGA4','DGB1','DGA8','DGC9','DGD1','DGD2','DGD4','DGC4','DGC2','DGD5','DGD6','DGC6','DGB9','DGC7','DGD7','DGD8','DGD9','DGE1','DGD3','DGE2','DGE6','DGE5','DGE7','DGF1','DGE8','DGE9','DGF2','DGF3','DGF4','DGF5','DGF6','DGF8','DGF7','DGF9','DGG1','DGG2','DGG3','DGG4','DGG5','DGG6','DGG8','DGG7','DGH1','DGH2','DGH8','DGH9','DGH4','DGH3','DGH5','DGH6','DGH7','DGJ5','DGJ2','DGJ3','DGJ4','DGJ7','DGJ8','DGJ9','DGJ6','DGK2','DGK3','DGK8','DGK9','DGK4','DGK1','DGK5','DGK6','DGL2','DGL3','DGK7','DGL1','DGL5','DGJ1','DGL8','DGL9','DGL4','DGM2','DGL6','DGL7','DGN2','DGM3','DGM4','DGM5','DGM1','DGM8','DG01','DGM9','DGM6','DGN1','DGM7','DG05','DG02','DG04','DG0A','DG03','DGN3','DG06','DG08','DG0C','DG09','DG0B','DG07','DG0E','DG0F','DG0G','DG0Q','DG0N','DG0H','DG0J','DG0K','DG0L','DG0U','DG0V','DG0M','DG0P','DG0R','DG0S','DG10','DG11','DG0T','DG0W','DG13','DG0X','DG0Y','DG14','DG15','DG0Z','DG12','DG17','DG18','DG1A','DG1B','DG19','DG16','DG1D','DG1E','DG1G','DG1H','DG1F','DG1C','DG1K','DG1L','DG1R','DG1S','DG1M','DG1J','DG1U','DG1Q','DG1T','DG1V','DG1W','DG1N','DG1P','DG1X','DG1Y','DG23','DG1Z','DG25','DG20','DG22','DG26','DG21','DG24','DG27','DG29','DG2A','DG2F','DG2G','DG2B','DG28','DG2C','DG2D','DG2J','DG2K','DG2E','DG2H','DG2M','DG2N','DG2Q','DG2R','DG2P','DG2L','DG2T','DG2U','DG2Z','DG2V','DG2S','DG2W','DG2X','DG35','DG36','DG2Y','DG31','DG32','DG33','DG3B','DG3C','DG34','DG37','DH1D','DH3U','DH4H','DH4U','DH5K','DH5R','DH5U','DH5V','DH5Z','DH6R','DH6V','DH6Z','DH7R','DH7V','DH7Z','DHJ7','H5LX','DH1A','DH1C','DH1E','DH1G','DH1H','DH1J','DH1K','DH1M','DH1P','DH1Z','DH2C','DH2F','DH2G','DH2H','DH2J','DH2K','DH2M','DH2N','DH2R','DH2S','DH2V','DH3A','DH3D','DH3F','DH3G','DH3J','DH3R','DH3V','DH3Y','DH3Z','DH4C','DH4F','DH4J','DH4N','DH4V','DH4X','DH4Y','DH5A','DH5C','DH5F','DH5J','DH5N','DH5P','DH5W','DH5X','DH5Y','DH6A','DH6F','DH6H','DH6J','DH6S','DH7H','DH7J','DH7L','DH7P','DH7S','DH7W','DH7Y','DH8A','DH8D','DH8H','DH8L','DH8N','DH8P','DH8Q','DH8S','DH8V','DH8W','DH8Y','DH8Z','DH9A','DH9B','DH9H','DH9J','DH9N','DH9P','DH9Q','DH9S','DH9U','DH9V','DH9Z','DHA0','DHA1','DHA2','DHA3','DHA4','DHA5','DHA6','DHAB','DHAK','DHB1','DHB2','DHB3','DHB4','DHB5','DHB6','DHB8','DHC7','DHD9','DHE5','DHE6','DHE8','DHE9','DHF0','DHF2','DHF3','DHF6','DHF9','DHG6','DHG7','DHG8','DHG9','DHH0','DHH1','DHH6','DHH7','DHH8','DHH9','DHI1','DHI3','DHI4','DHI5','DHI6','DHI7','DHI8','DHJ1','DHJ5','DHJ6','DHK2','DHK8','DHK9','DHL0','DHL1','DHL2','DHL3','DHM5','DHM6','DHN2','DHN5','DHP9','DHQ8','DHR1','DHR9','DHS7','DHS9','DHT2','DHT3','DHT9','DHU1','DHU6','DHU7','DHU8','DHU9','DHW2','DHW3','DHW4','DHW5','DHW6','DHW7','DHW8','DHW9','DHX1','DHX3','DHX4','DHX5','DHY1','DHY2','DHY3','DHY4','DHY5','DHY6','DHZ2','DHZ3','DHZ4','DHZ6','DHZ7','DHZ8','DH8M','DH9M','DHAA','DHR6','DH1R','DH2E','DH3M','DH4S','DH6M','DH6X','DH8B','DH9D','DH9R','DH9W','DHAF','DHAG','DHAH','DHAI','DHAJ','DHAL','DHE4','DHI9','DHM7','DHAO','DHAP','DHAQ','DHN7','DHP2','DH1L','DH2X','DH3C','DH3N','DH6N','DH6Y','DH7N','DHAC','DHAM','DHAN','DHAU','DHAV','DHAW','DHAX','DHAY','DHBE','DHBF','DHBR','DHY9','DHBZ','DHCA','DHCB','DH2P','DH3P','DH4D','DH4Z','DHBG','DHBH','DHBJ','DHBT','DHBW','DHBX','DHC0','DHCG','DHCJ','DHCR','DHDW','DHE0','DHN6','DHP5','DHU2','DHDT','DHDU','DHDV','DH1V','DH5E','DH6E','DH7M','DH7X','DHBA','DHBB','DHCK','DHCL','DHCM','DHCN','DHCP','DHEE','DHEF','DHEA','DHEB','DHEC','DH1N','DH2L','DH4G','DHDY','DHEH','DHEQ','DHEV','DH2D','DHER','DHES','DHET','DH1W','DH4P','DH5S','DH6Q','DHBC','DHBV','DHBY','DHCF','DHEW','DHEX','DHEY','DHFH','DHFJ','DHFK','DH1S','DHAT','DHAZ','DHEK','DHEZ','DHFA','DHFB','DHFC','DHFD','DHFF','DHFL','DHFR','DHFS','DHFT','DHFY','DHFZ','DHGC','DHGD','DHGH','DHGJ','DHGK','DHGS','DHGW','DH1X','DHFP','DHGL','DHGT','DHGZ','DHHA','DHHB','DHHC','DHHE','DHN4','DHGV','DHGX','DHHM','DH2T','DHGF','DHGY','DHHH','DHHJ','DHHK','DHHQ','DHHR','DHHS','DHHT','DHHL','DHHN','DHJK','DHFM','DHHF','DHJE','DHJF','DHJG','DHJL','DHJQ','DHJH','DHJJ','DHCD','DHGE','DHJR','DHJV','DHKB','DHJW','DHJX','DHJY','DHKG','DHKX','DHKD','DHKK','DHKY','DHKT','DHKV','DHKW','DHLM','DH3L','DH7B','DHBD','DHFG','DHKL','DHKN','DHKP','DHKQ','DHKS','DHLB','DHLE','DHX9','DHLJ','DHLK','DHLL','DHNG','DHCC','DHGA','DHJS','DHJZ','DHLA','DHLG','DHLH','DHLT','DHLY','DHLZ','DHNB','DHNJ','DHNK','DHND','DHNE','DHNF','DHNV','DHLF','DHLU','DHMA','DHMW','DHMY','DHNP','DHNY','DHPB','DHPD','DHPK','DHPP','DHNR','DHNS','DHNT','DHRG','DHLN','DHLW','DHMV','DHNL','DHNW','DHNX','DHPE','DHPM','DHPS','DHPZ','DHQB','DHQP','DHQR','DHRC','DHRH','DHRJ','DHRK','DHRL','DHRM','DHRN','DHRD','DHRE','DHRF','DHRW','DHFV','DHNM','DHQD','DHSD','DHRT','DHRU','DHRV','DHSC','DHSN','DHSP','DHMK','DHMM','DHNZ','DHPA','DHRS','DHRX','DHRZ','DHSA','DHSG','DHSH','DHSJ','DHSQ','DHSS','DHST','DHSU','DHSV','DHSW','DHSX','DHSY','DHTA','DHTB','DHTC','DHTD','DHTE','DHTL','DHTM','DHSK','DHSL','DHSM','DHTJ','DHTK','DHTR','DHPV','DHQH','DHQY','DHRA','DHSE','DHSZ','DHTN','DHTP','DHTQ','DHTS','DHVK','DHTF','DHTG','DHTH','DHVV','DHVW','DHMC','DHNN','DHNQ','DHQF','DHQT','DHQW','DHSF','DHTW','DHVY','DHVZ','DHY8','DHUD','DHUE','DHVS','DHVT','DHVU','DHBN','DHKF','DHLS','DHMX','DHQJ','DHUF','DHUG','DHUH','DHUU','DHVB','DHVD','DHVX','DHUA','DHUB','DHUC','DHUW','DHUY','DHUZ','DHAD','DHHG','DHLV','DHPX','DHWC','DHWE','DHWF','DHWG','DHUV','DHUX','DHWR','DH03','DHQE','DHQM','DHUT','DHVE','DHWD','DHWL','DHWM','DHWN','DHWP','DHWQ','DHWS','DH06','DH07','DH0C','DH0H','DH0N','DHP8','DHTV','DHTZ','DH0U','DH08','DH09','DH0A','DH0B','DH0J','DH0P','DH0Z','DH10','DH11','DH12','DH13','DH14','DH15','DHQN','DHQV','DH0S','DH0T','DH0W','DH49','DH04','DH0M','DH16','DH51','DH52','DH53','DH54','DH56','DHRP','DHVC','DH46','DH47','DH48','DH62','DH0D','DH0E','DH0F','DH0R','DH0X','DH25','DH35','DH57','DH58','DH7C','DHQZ','DHVA','DH59','DH60','DH61','DH85','DH87','DH20','DH21','DH64','DH74','DH75','DH76','DH77','DH78','DH81','DH82','DH92','DH93','DH94','DH95','DH96','DHVP','DH84','DH86','DH91','DHFU','DHH2','DHHU','DH0G','DH55','DHJU','DHP0','DHPF','DHPU','DHQ0','DHQU','DHR0','DHTU','DHHD','DHHX','DHWW','DH0Y','DH9X','DHK0','DHKU','DHLC','DHLD','DHLP','DHM0','DHS0','DHT0','DHXA','DHXB','DHXC','DHXD','DHXE','DHGU','DHWU','DHWV','DHXJ','DH17','DH18','DHX0','DHXL','DHXM','DHXN','DHXP','DHXQ','DHXR','DHXS','DHXT','DHXU','DHXG','DHXH','DHXK','DHXW','DHXY','DH19','DH37','DH39','DHN0','DHNU','DHY0','DHYC','DHYD','DHYG','DHYH','DHXX','DHXZ','DHYM','DHYP','DHRR','DHYJ','DHYL','DHYS','DH99','DHYN','DHYQ','DHYU','DHYW','DH0L','DH4R','DHMT','DHWZ','DHXV','DHYV','DHYX','DHZE','DHZG','DHPQ','DHRQ','DHZL','DHZF','DHZH','DHZV','DHZX','DHMU','DHQQ','DHTT','DHXF','DHZM','DHZN','DHZQ','DHZR','DHZS','DHZZ','VH04','VH05','DHZW','DHZY','VH09','VH0B','DHU0','DHVJ','DHWA','VH01','VH02','VH07','DH00','VH0A','VH0C','VH0L','VH0P','DH22','DH3S','DH4W','DHTX','DHUS','DHZT','VH03','VH0F','VH0G','VH0H','VH0K','VH0Q','VH0R','VH0W','VH0M','VH0N','VH14','VH16','DH4M','DHAE','DHPH','VH0T','VH0U','VH0V','VH0Z','VH10','VH1A','VH1D','VH1E','VH1K','VH15','VH17','VH1L','VH1N','VH1U','VH1M','VH1P','VH1V','VH1W','DH8K','DHBM','VH0J','VH0X','VH1Q','VH1S','VH1Y','DBE7','DBE8','DBE9','DBA0','DBA3','DBA9','DBB3','DBB4','DBB5','DBC0','DBE4','DBE5','DBF1','DBE1','DBF2','DBF3','DBF4','DBF5','DBF6','DBF7','DBF8','DBG1','DBG2','DBG3','DBG4','DBG5','DBG6','DBG7','DBG8','DBG9','DBH1','DBH2','DBH3','DBH4','DBH5','DBH6','DBH7','DBH8','DBH9','DBJ1','DBJ2','DBJ3','DBJ4','DBJ5','DBJ6','DBJ7','DBJ8','DBJ9','DBK1','DBK2','DBK3','DBK4','DBK5','DBK6','DBK7','DBK8','DBK9','DBL1','DBL2','DBL3','DBL4','DBL5','DBL6','DBL7','DBL8','DBL9','DBM1','DBM2','DB01','DB02','DB03','DB06','DB04','DB05','DB07','DB08','DB09','DB0A','DB0B','DB0C','DB0D','DB0E','DB0F','DB0G','DB0H','DB0J','DB0K','DB0L','DB0M','DB0N','DB0P','DB0Q','DB0R','DB0S','DB0T','DB0U','DB0V','DB0W','DB0X','DB0Y','DB0Z','DB10','DB11','DB12','DB13','DB14','DB15','DB16','DB17','DB18','DB19','DB1A','DB1B','DB1C','DB1D','DB1E','DB1F','DB1G','DB1H','DB1J','DB1K','DB1L','DB1M','DB1N')
)
ARRAY JOIN platform_order_no_array AS platform_order_no_new, arrayEnumerate(platform_order_no_array) AS i; 

-- 数据验证
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_change_mt WHERE platform_order_no LIKE '%,%';
SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_change_mt WHERE platform_order_no LIKE '%2686188457581570647%';
SELECT * FROM custom_online_sale_change_local WHERE platform_order_no IN ('2686188457618570647','2686188457572570647','2686188457581570647');


-- 按照下单时间
SELECT 
	a.perTime, 
	a.store_code, 
	MAX(a.store_name), 
	SUM(a.sale_amount), 
	SUM(b.sale_refund_amount), 
	SUM(c.sale_change_amount), 
	SUM(d.order_freight_amt),
	SUM(f.return_freight_amt)
FROM (SELECT
    SUBSTR(order_date, 1, 7) AS perTime,
	store_code,
	MAX(store_name) AS store_name,
	platform_order_no,
	SUM(goods_amt) AS sale_amount
FROM custom_online_sale_local
GROUP BY SUBSTR(order_date, 1, 7), store_code, platform_order_no) a
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(order_freight_amt_t) AS order_freight_amt
	FROM (SELECT
			store_code,
			system_order_no,
			platform_order_no,
			MAX(order_freight_amt) AS order_freight_amt_t
		FROM custom_online_sale_local
		WHERE source_system = 'EC_HIS' AND order_freight_amt > 0
		GROUP BY store_code, system_order_no, platform_order_no
	) GROUP BY store_code, platform_order_no
) d ON a.store_code = d.store_code AND a.platform_order_no = d.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(return_goods_amt) AS sale_refund_amount
	FROM custom_online_sale_return_local
	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, MAX(return_freight_amt) AS return_freight_amt
	FROM custom_online_sale_return_local
	WHERE source_system = 'EC_HIS'
	GROUP BY store_code, platform_order_no
) f ON a.store_code = f.store_code AND a.platform_order_no = f.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(special_barcode_amt) AS sale_change_amount
	FROM custom_online_sale_change_local
	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.perTime, a.store_code;

-- 按照钱货两清时间
SELECT 
	a.perTime, 
	a.store_code, 
	MAX(a.store_name), 
	SUM(a.sale_amount), 
	SUM(b.sale_refund_amount), 
	SUM(c.sale_change_amount), 
	SUM(d.order_freight_amt),
	SUM(f.return_freight_amt)
FROM (SELECT
    SUBSTR(mgclear_time, 1, 7) AS perTime,
	store_code,
	MAX(store_name) AS store_name,
	platform_order_no,
	SUM(goods_amt) AS sale_amount
FROM custom_online_sale_local
GROUP BY SUBSTR(mgclear_time, 1, 7), store_code, platform_order_no) a
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(order_freight_amt_t) AS order_freight_amt
	FROM (SELECT
			store_code,
			system_order_no,
			platform_order_no,
			MAX(order_freight_amt) AS order_freight_amt_t
		FROM custom_online_sale_local
		WHERE source_system = 'EC_HIS' AND order_freight_amt > 0
		GROUP BY store_code, system_order_no, platform_order_no
	) GROUP BY store_code, platform_order_no
) d ON a.store_code = d.store_code AND a.platform_order_no = d.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(return_goods_amt) AS sale_refund_amount
	FROM custom_online_sale_return_local
	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, MAX(return_freight_amt) AS return_freight_amt
	FROM custom_online_sale_return_local
	WHERE source_system = 'EC_HIS'
	GROUP BY store_code, platform_order_no
) f ON a.store_code = f.store_code AND a.platform_order_no = f.platform_order_no
LEFT JOIN (
	SELECT store_code, platform_order_no, SUM(special_barcode_amt) AS sale_change_amount
	FROM custom_online_sale_change_local
	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.perTime, a.store_code;

SELECT
	store_code,
    SUBSTR(mgclear_time, 1, 7) AS perTime,
	MAX(store_name) AS store_name,
	SUM(goods_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_code = 'DHF3' AND SUBSTR(mgclear_time, 1, 4) = '2024'
GROUP BY store_code, SUBSTR(mgclear_time, 1, 7);

SELECT
	store_code,
    SUBSTR(mgclear_time, 1, 7) AS perTime,
	SUM(return_goods_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
WHERE store_code = 'DHF3' AND SUBSTR(mgclear_time, 1, 4) = '2024'
GROUP BY store_code, SUBSTR(mgclear_time, 1, 7);

SELECT
	store_code,
    SUBSTR(mgclear_time, 1, 7) AS perTime,
	SUM(special_barcode_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
WHERE store_code = 'DHF3' AND SUBSTR(mgclear_time, 1, 4) = '2024'
GROUP BY store_code, SUBSTR(mgclear_time, 1, 7);


SELECT a.perTime, 
	a.store_code, 
	MAX(a.store_name), 
	SUM(a.sale_amount), 
	SUM(b.sale_refund_amount)
FROM (SELECT
	store_code,
    SUBSTR(mgclear_time, 1, 7) AS perTime,
    platform_order_no,
	MAX(store_name) AS store_name,
	SUM(goods_amt) AS sale_amount
FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_code = 'DHF3' AND SUBSTR(mgclear_time, 1, 4) = '2024'
GROUP BY store_code, SUBSTR(mgclear_time, 1, 7), 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
GROUP BY a.store_code, a.perTime;

SELECT COUNT() FROM  dwd_trade_hkaudit_ecommerce_sale_mt WHERE order_time < '2022-01-01 00:00:00';

SELECT COUNT() FROM dwd_trade_hkaudit_ecommerce_sale_mt 
WHERE store_code = 'DHF3' AND order_time >= '2024-05-01 00:00:00' AND order_time < '2024-08-01 00:00:00';

SELECT COUNT(DISTINCT system_order_no) FROM dwd_trade_hkaudit_ecommerce_sale_mt 
WHERE store_code = 'DHF3' AND order_time >= '2024-05-01 00:00:00' AND order_time < '2024-08-01 00:00:00' AND order_freight_amt <> 0;


SELECT COUNT() FROM dwd_trade_hkaudit_ecommerce_sale_change_mt
WHERE store_code = 'DHF3' AND create_date >= '2024-05-01' AND create_date < '2024-08-01';


SELECT COUNT() FROM dwd_trade_hkaudit_ecommerce_sale_return_mt
WHERE store_code = 'DHF3' AND create_date >= '2024-05-01' AND create_date < '2024-08-01';


SELECT * FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_name LIKE '%唯品会%' AND order_date >= '2024-05-01' AND order_date < '2024-08-01';

SELECT order_date, goods_barcode  FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE store_code = 'DB0K' GROUP BY order_date, goods_barcode HAVING COUNT(goods_barcode) > 1;

SELECT consignee_add FROM dwd_trade_hkaudit_ecommerce_sale_mt;

SELECT COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt;
SELECT COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE consignee_add = '';


SELECT COUNT()  FROM dwd_trade_hkaudit_ecommerce_sale_mt WHERE consignee_add = '' AND (store_name LIKE '%唯品会%' OR store_name LIKE '%京东自营%');

SELECT store_code, MAX(store_name), COUNT() FROM dwd_trade_hkaudit_ecommerce_sale_mt
WHERE consignee_add = '' AND store_name NOT LIKE '%唯品会%' AND store_name NOT LIKE '%京东自营%'
GROUP BY store_code ;





