KA 接口表

1、建表python

一、年日均銷量表sql

drop table app.app_basic_dashboard_goods_avg_year_sellnum;
CREATE TABLE app.app_basic_dashboard_goods_avg_year_sellnum (
  seller_id bigint COMMENT '商家id', 
  seller_name string COMMENT '商家名稱', 
  dept_id bigint COMMENT '事業部門id', 
  dept_name string COMMENT '事業部名字', 
  warehouse_id bigint COMMENT '入倉id', 
  warehouse_name string COMMENT '入倉名稱',
  goods_id bigint COMMENT '商品id', 
  goods_no string COMMENT '商品序號', 
  goods_name string COMMENT '商品名稱', 
  avg_year_sellnum float COMMENT '年日均銷量',
  yn int COMMENT '刪除標識 1爲沒刪,0爲刪除', 
  create_pin string COMMENT '建立人', 
  update_pin string COMMENT '更新人', 
  create_time timestamp COMMENT '建立時間', 
  update_time timestamp COMMENT '更新時間', 
  ts timestamp COMMENT '時間戳')
COMMENT 'KA商品銷量預測年日均銷量'
PARTITIONED BY ( 
  dt string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

 二、商家參數表apache

CREATE EXTERNAL TABLE `app_basic_dashboard_goods_seller`(
  `seller_id` string COMMENT '商家id', 
  `seller_no` string COMMENT '商家編號', 
  `seller_name` string COMMENT '商家名稱', 
  `dept_id` string COMMENT '部門id', 
  `dept_no` string COMMENT '部門編號', 
  `vlt` string COMMENT 'VLT',
  `alt` string COMMENT 'ALT',
  `satisfyAlpha` string COMMENT '出倉服務水平C',
  `safetyDays` int COMMENT '安全庫存天數',
  `targetDays` int COMMENT '目標庫存天數',
  `bp` string COMMENT 'BP',
  `task_exec_date` string COMMENT '任務執行時的時間,保存格式爲:2018-03-31', 
  `yn` string COMMENT '刪除標識 1爲沒刪,0爲刪除', 
  `create_time` timestamp COMMENT '建立時間', 
  `create_pin` string COMMENT '建立人', 
  `update_pin` string COMMENT '更新人', 
  `update_time` timestamp COMMENT '更新時間', 
  `ts` timestamp COMMENT '時間戳')
COMMENT '商家表'
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

三、KA 臨時中轉表安全

drop table app.app_basic_dashboard_goods_sale_predict_daily_tem;
CREATE TABLE app.app_basic_dashboard_goods_sale_predict_daily_tem (
  seller_id bigint COMMENT '商家id', 
  seller_name string COMMENT '商家名稱', 
  dept_id bigint COMMENT '事業部門id', 
  dept_no string COMMENT '事業部門編號',  
  dept_name string COMMENT '事業部名字', 
  goods_id bigint COMMENT '商品id', 
  goods_no string COMMENT '商品序號', 
  goods_name string COMMENT '商品名稱', 
  warehouse_id bigint COMMENT '入倉id', 
  warehouse_no string COMMENT '入倉編號', 
  warehouse_name string COMMENT '入倉名稱', 
  in_warehouse_city string COMMENT '入倉城市',
  satisfy_alpha string COMMENT '入倉服務水平C', 
  safe_stock_days string COMMENT '安全庫存天數', 
  alt string COMMENT '出倉ALT', 
  vlt string COMMENT '出倉VLT', 
  in_stock_safety_num int COMMENT '安全庫存', 
  target_stock_days string COMMENT '目標庫存天數', 
  bp string COMMENT 'BP', 
  out_warehouse_no string COMMENT '出倉編號', 
  out_warehouse_name string COMMENT '出倉名稱', 
  out_warehouse_city string COMMENT '出倉城市', 
  in_stock_max_num int COMMENT '目標庫存', 
  bef_sales_1d string COMMENT 'T-1日銷量', 
  bef_sales_2d string COMMENT 'T-2日銷量', 
  bef_sales_3d string COMMENT 'T-3日銷量',
  bef_sales_4d string COMMENT 'T-4日銷量', 
  bef_sales_5d string COMMENT 'T-5日銷量', 
  bef_sales_6d string COMMENT 'T-6日銷量',
  bef_sales_7d string COMMENT 'T-7日銷量', 
  in_stock_sales_14d string COMMENT '歷史14日銷量和', 
  in_stock_sales_28d string COMMENT '歷史28日銷量和',
  predict_sales_1d string COMMENT 'T+1日預測銷量',
  predict_sales_2d string COMMENT 'T+2日預測銷量', 
  predict_sales_3d string COMMENT 'T+3日預測銷量',
  predict_sales_4d string COMMENT 'T+4日預測銷量',
  predict_sales_5d string COMMENT 'T+5日預測銷量', 
  predict_sales_6d string COMMENT 'T+6日預測銷量',
  predict_sales_7d string COMMENT 'T+7日預測銷量',
  in_stock_predict_sales_14d string COMMENT '14日預測銷量和', 
  in_stock_predict_sales_28d string COMMENT '28日預測銷量和',
  yn int COMMENT '刪除標識 1爲沒刪,0爲刪除', 
  create_pin string COMMENT '建立人', 
  update_pin string COMMENT '更新人', 
  create_time timestamp COMMENT '建立時間', 
  update_time timestamp COMMENT '更新時間', 
  ts timestamp COMMENT '時間戳')
COMMENT 'KA商品銷量預測接口中轉表'
PARTITIONED BY ( 
  dt string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

 四、KA 接口表app

drop table app.app_basic_dashboard_goods_sale_predict_daily;
CREATE TABLE app.app_basic_dashboard_goods_sale_predict_daily (
  seller_id bigint COMMENT '商家id', 
  seller_name string COMMENT '商家名稱', 
  dept_id bigint COMMENT '事業部門id', 
  dept_no string COMMENT '事業部門編號',  
  dept_name string COMMENT '事業部名字', 
  goods_id bigint COMMENT '商品id', 
  goods_no string COMMENT '商品序號', 
  goods_name string COMMENT '商品名稱', 
  warehouse_id bigint COMMENT '入倉id', 
  warehouse_no string COMMENT '入倉編號', 
  warehouse_name string COMMENT '入倉名稱', 
  in_warehouse_city string COMMENT '入倉城市',
  satisfy_alpha string COMMENT '入倉服務水平C', 
  safe_stock_days string COMMENT '安全庫存天數', 
  alt string COMMENT '出倉ALT', 
  vlt string COMMENT '出倉VLT', 
  in_stock_safety_num int COMMENT '安全庫存', 
  target_stock_days string COMMENT '目標庫存天數', 
  bp string COMMENT 'BP', 
  out_warehouse_no string COMMENT '出倉編號', 
  out_warehouse_name string COMMENT '出倉名稱', 
  out_warehouse_city string COMMENT '出倉城市', 
  in_stock_max_num int COMMENT '目標庫存', 
  bef_sales_1d string COMMENT 'T-1日銷量', 
  bef_sales_2d string COMMENT 'T-2日銷量', 
  bef_sales_3d string COMMENT 'T-3日銷量',
  bef_sales_4d string COMMENT 'T-4日銷量', 
  bef_sales_5d string COMMENT 'T-5日銷量', 
  bef_sales_6d string COMMENT 'T-6日銷量',
  bef_sales_7d string COMMENT 'T-7日銷量', 
  in_stock_sales_14d string COMMENT '歷史14日銷量和', 
  in_stock_sales_28d string COMMENT '歷史28日銷量和',
  predict_sales_1d string COMMENT 'T+1日預測銷量',
  predict_sales_2d string COMMENT 'T+2日預測銷量', 
  predict_sales_3d string COMMENT 'T+3日預測銷量',
  predict_sales_4d string COMMENT 'T+4日預測銷量',
  predict_sales_5d string COMMENT 'T+5日預測銷量', 
  predict_sales_6d string COMMENT 'T+6日預測銷量',
  predict_sales_7d string COMMENT 'T+7日預測銷量',
  in_stock_predict_sales_14d string COMMENT '14日預測銷量和', 
  in_stock_predict_sales_28d string COMMENT '28日預測銷量和',
  yn int COMMENT '刪除標識 1爲沒刪,0爲刪除', 
  create_pin string COMMENT '建立人', 
  update_pin string COMMENT '更新人', 
  create_time timestamp COMMENT '建立時間', 
  update_time timestamp COMMENT '更新時間', 
  ts timestamp COMMENT '時間戳')
COMMENT 'KA商品銷量預測接口表'
PARTITIONED BY ( 
  dt string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

2、插值oop

一、商家參數表編碼

insert overwrite table app.app_basic_dashboard_goods_seller
select distinct
	seller_id,
	seller_no,
	seller_name,
	dept_id,
	dept_no,
    '36' as vlt,  --VLT
    '10' as alt,  --ALT
    '0.8' as satisfyAlpha,  --出倉服務水平C
	case
		when seller_no = 'ECP0020000003619'  --安利
		then 32
		when seller_no = 'ECP0020000014466'  --住友
		then 120
		else 0
	end as safetyDays,  --安全庫存天數
	case
		when seller_no = 'ECP0020000003619'  
		then 40
		when seller_no = 'ECP0020000014466'  
		then 132
		else 0
	end as targetDays,  --目標庫存天數
    '20' as bp,  --BP
	'"""+yesterday+"""' AS task_exec_date,
	1 AS yn,
	current_timestamp AS create_time,
	'plumber' AS create_pin,
	'plumber' AS update_pin,
	current_timestamp AS update_time,
	current_timestamp AS ts
from
	fdm.fdm_eclp_so1_so_main_chain
WHERE
	start_date <= '"""+yesterday+"""'
	and end_date > '"""+yesterday+"""'
	and seller_no in('ECP0020000003619', 'ECP0020000014466') ;
   

二、年日均銷量表3d

#!/usr/bin/env python3
################################################################
# AUTHOR:         wn
# CREATED TIME:   2018-08-09
# MODIFIED BY:    
# MODIFTED TIME:  
# REVIEWED BY:    
# REVIEWED TIME:  
# COMMENTS: goods

################################################################
#===============================================================================
#  FILE: exe_app_basic_dashboard_goods_avg_year_sellnum.py
#  USAGE: ./exe_app_basic_dashboard_goods_avg_year_sellnum.py
#  SRC_TABLE: 
#  TGT_TABLE: app.app_basic_dashboard_goods_avg_year_sellnum
#===============================================================================
import sys
import os
import time
import datetime
import logging 
import calendar

sys.path.append(os.getenv('HIVE_TASK'))
from HiveTask import HiveTask
ht = HiveTask()
today = ht.oneday(1)[0:10]
yesterday  = ht.oneday(0)[0:10] 


sql1 = """
use app;
	insert overwrite table app.app_basic_dashboard_goods_avg_year_sellnum partition
	(
	   dt = '"""+yesterday+"""'
	) 
	SELECT
	seller_id, --商家id
	seller_name, 
	dept_id,
	dept_name,
	warehouse_id,
	warehouse_name,
	goods_id,
	goods_no,
	goods_name,
	round((
			case
				WHEN saletime >= 365
				THEN yreal_outtore_qty / 365
				WHEN saletime < 365
				THEN real_outtore_qty / datediff(sysdate( - 1), oldsaletime)
				ELSE 0
			end), 2) AS avg_year_sellnum, --年日均銷量
	1 AS yn, --刪除標識 1爲沒刪,0爲刪除
	'plumber' AS create_pin, --建立人
	'plumber' AS update_pin, --更新人
	current_timestamp AS create_time, --建立時間
	current_timestamp AS update_time, --更新時間
	current_timestamp AS ts --時間戳
FROM
	(
		SELECT
			seller_id,
			seller_name,
			dept_id,
			dept_name,
			warehouse_id,
			warehouse_name,
			goods_id,
			goods_no,
			goods_name,
			datediff(sysdate( - 1), min(to_date(create_time))) AS saletime, --
			min(to_date(create_time)) AS oldsaletime, --
			sum(real_outtore_qty) AS real_outtore_qty, --
			sum(
				case
					WHEN create_time >= date_sub(sysdate( - 1), 365)
					THEN real_outtore_qty
					ELSE 0
				end) AS yreal_outtore_qty --
		FROM
			(
				SELECT
					main.seller_id,
					main.seller_name,
					main.dept_id,
					main.dept_name,
					main.warehouse_id,
					case
						when warehouse.warehouse_name is not null
						then warehouse.warehouse_name
						else main.warehouse_name
					end warehouse_name,
					item.create_time, --
					item.goods_id,
					item.goods_no,
					item.goods_name,
					item.real_outtore_qty AS real_outtore_qty
				FROM
					(
						SELECT
							so_no,
							seller_id,
							seller_no,
							seller_name,
							dept_id,
							dept_no,
							trim(dept_name) as dept_name,
							warehouse_id,
							warehouse_no,
							warehouse_name
						FROM
							fdm.fdm_eclp_so1_so_main_chain
						WHERE
							start_date <= sysdate( - 1)
							and end_date > sysdate( - 1)
							AND seller_no in
							(
								select seller_no from app.app_basic_dashboard_goods_seller
							)
							AND
							(
								parent_id = cast(substring(so_no, 4) AS bigint)
								OR parent_id is NULL
							)
							AND so_status <> '10056'
							AND so_status <> '10009'
							AND so_status <> '10028'
							AND so_status <> '10060'
					)
					main
				JOIN
					(
						SELECT
							so_id,
							goods_id,
							goods_no,
							goods_name,
							dept_id,
							sum(nvl(apply_outstore_qty, 0)) AS apply_outstore_qty,
							sum(
								case
									WHEN nvl(real_outtore_qty, 0) = 0
										and nvl(apply_outstore_qty, 0) > 0
									THEN nvl(apply_outstore_qty, 0)
									ELSE nvl(real_outtore_qty, 0)
								end) AS real_outtore_qty,
							min(create_time) as create_time
						FROM
							fdm.fdm_eclp_so1_so_item_chain
						WHERE
							dt >= date_sub(sysdate( - 1), 365)
						GROUP BY
							so_id,
							goods_id,
							goods_no,
							goods_name,
							dept_id
					)
					item
				ON
					substring(main.so_no, 4) = item.so_id
				LEFT JOIN
					(
						SELECT
							warehouse_no,
							warehouse_name
						from
							app.app_log_scm_ka_warehouse wh1
						left join
							(
								select distinct
									dim_area_id,
									dim_area_name
								from
									dim.dim_supp_report_area_province
							)
							wh2
						on
							trim(wh1.org_name) = trim(wh2.dim_area_name)
						GROUP BY
							org_id,
							wh2.dim_area_id,
							org_name,
							warehouse_no,
							warehouse_name,
							province_id,
							province_name,
							city_id,
							city_name
					)
					warehouse ON main.warehouse_no = warehouse.warehouse_no
			)
			p
		GROUP BY
			seller_id,
			seller_name,
			dept_id,
			dept_name,
			warehouse_id,
			warehouse_name,
			goods_id,
			goods_no,
			goods_name
	)
	q
    
             
"""

ht.exec_sql(schema_name = 'app', table_name = 'app_basic_dashboard_goods_avg_year_sellnum', sql = sql1, merge_flag = True) 

三、中轉表orm

#!/usr/bin/env python3
################################################################
# AUTHOR:         wn
# CREATED TIME:   2018-08-09
# MODIFIED BY:    
# MODIFTED TIME:  
# REVIEWED BY:    
# REVIEWED TIME:  
# COMMENTS: goods

################################################################
#===============================================================================
#  FILE: exe_app_basic_goods_stock_num_daily_da_d.py
#  USAGE: ./exe_app_basic_goods_stock_num_daily_da_d.py
#  SRC_TABLE: 
#  TGT_TABLE: app_basic_goods_stock_num_daily_da
#===============================================================================
import sys
import os
import time
import datetime
import logging 
import calendar

sys.path.append(os.getenv('HIVE_TASK'))
from HiveTask import HiveTask
ht = HiveTask()
today = ht.oneday(1)[0:10]
yesterday  = ht.oneday(0)[0:10] 


sql1 = """
use app;
	insert overwrite table app.app_basic_dashboard_goods_sale_predict_daily_tem partition
	(
	   dt = '"""+yesterday+"""'
	) 
	select
	c.seller_id, --商家編號
	c.seller_name, --商家名稱
	c.dept_id, --事業部id
	c.dept_no, --事業部編號
	c.dept_name, --事業部名稱
	c.goods_id, --商品id
	c.goods_no, --商品編號
	c.goods_name, --商品名稱
	c.warehouse_id, --入倉id
	c.warehouse_no, --入倉編號
	c.warehouse_name, --入倉名稱
	h.loc_city_name as in_warehouse_city, --入倉城市
	case
		when d.satisfy_alpha is null
		then n.satisfyalpha
		else d.satisfy_alpha
	end as satisfy_alpha, -- 出倉服務水平c (basis)
	case
		when d.safe_stock_days is null
		then n.safetydays
		else d.safe_stock_days
	end as safe_stock_days, -- 安全庫存天數(basis)
	case
		when d.alt is null
		then n.alt
		else d.alt
	end as alt, -- 出倉alt(小時)(basis)
	case
		when j.totaltime is null
		then n.vlt
		else j.totaltime
	end as vlt, --運輸時間(basis)
	f.in_stock_safety_num as in_stock_safety_num, --安全庫存(計劃調撥表取數)
	case
		when d.target_stock_days is null
		then n.targetdays
		else d.target_stock_days
	end as target_stock_days, -- 目標庫存天數(basis)
	case
		when d.bp is null
		then n.bp
		else d.bp
	end as BP, -- BP
	f.out_warehouse_no as out_warehouse_no, --配出倉編碼
	f.out_warehouse_name as out_warehouse_name, --配出倉名稱
	i.loc_city_name as out_warehouse_city, --入倉城市
	f.in_stock_max_num as in_stock_max_num, --目標庫存(計劃調撥表取數)
	case
		when split(g.sales_week_detail, ',') [6] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [6]
	end as bef_sales_1d, --T-1日銷量(逆向)
	case
		when split(g.sales_week_detail, ',') [5] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [5]
	end as bef_sales_2d, --T-2日銷量
	case
		when split(g.sales_week_detail, ',') [4] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [4]
	end as bef_sales_3d, --T-3日銷量
	case
		when split(g.sales_week_detail, ',') [3] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [3]
	end as bef_sales_4d, --T-4日銷量
	case
		when split(g.sales_week_detail, ',') [2] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [2]
	end as bef_sales_5d, --T-5日銷量
	case
		when split(g.sales_week_detail, ',') [1] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [1]
	end as bef_sales_6d, --T-6日銷量
	case
		when split(g.sales_week_detail, ',') [0] is NULL
		then '0'
		else split(g.sales_week_detail, ',') [0]
	end as bef_sales_7d, --T-7日銷量
	case
		when g.sales_14d is NULL
		then '0'
		else g.sales_14d
	end as in_stock_sales_14d, --14日銷量
	case
		when g.sales_28d is NULL
		then '0'
		else g.sales_28d
	end as in_stock_sales_28d, --28日銷量
	case
		when split(g.predict_sales_week_detail, ',') [0] is NULL
			and m.avg_year_sellnum is NULL --都空爲0
		then '0'
		when split(g.predict_sales_week_detail, ',') [0] is NULL
			and m.avg_year_sellnum is not NULL --沒有預測值,按銷量平均值計
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [0] --預測值計
	end as predict_sales_1d, --T+1日預測銷量(正向)
	case
		when split(g.predict_sales_week_detail, ',') [1] is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when split(g.predict_sales_week_detail, ',') [1] is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [1]
	end as predict_sales_2d, --T+2日預測銷量
	case
		when split(g.predict_sales_week_detail, ',') [2] is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when split(g.predict_sales_week_detail, ',') [2] is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [2]
	end as predict_sales_3d, --T+3日預測銷量
	case
		when split(g.predict_sales_week_detail, ',') [3] is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when split(g.predict_sales_week_detail, ',') [3] is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [3]
	end as predict_sales_4d, --T+4日預測銷量
	case
		when split(g.predict_sales_week_detail, ',') [4] is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when split(g.predict_sales_week_detail, ',') [4] is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [4]
	end as predict_sales_5d, --T+5日預測銷量
	case
		when split(g.predict_sales_week_detail, ',') [5] is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when split(g.predict_sales_week_detail, ',') [5] is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [5]
	end as predict_sales_6d, --T+6日預測銷量
	case
		when split(g.predict_sales_week_detail, ',') [6] is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when split(g.predict_sales_week_detail, ',') [6] is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum)
		else split(g.predict_sales_week_detail, ',') [6]
	end as predict_sales_7d, --T+7日預測銷量
	case
		when g.predict_sales_14d is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when g.predict_sales_14d is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum * 14)
		else g.predict_sales_14d
	end as in_stock_predict_sales_14d, --14日預測銷量
	case
		when g.predict_sales_28d is NULL
			and m.avg_year_sellnum is NULL
		then '0'
		when g.predict_sales_28d is NULL
			and m.avg_year_sellnum is not NULL
		then round(m.avg_year_sellnum * 28)
		else g.predict_sales_28d
	end as in_stock_predict_sales_28d, --28日預測銷量
	1 AS yn, --刪除標識 1爲沒刪,0爲刪除
	'plumber' AS create_pin, --建立人
	'plumber' AS update_pin, --更新人
	current_timestamp AS create_time, --建立時間
	current_timestamp AS update_time, --更新時間
	current_timestamp AS ts --時間戳
from
	(
		SELECT
			*
		FROM
			(
				SELECT
					id,
					goods_id,
					goods_no,
					goods_name,
					seller_id,
					seller_no,
					trim(seller_name) as seller_name, --去除空格
					dept_id,
					dept_no,
					trim(dept_name) as dept_name,
					warehouse_no,
					warehouse_id,
					warehouse_name,
					update_time,
					create_time,
					row_number() over(partition by goods_id, seller_no, warehouse_no ORDER BY update_time desc, create_time desc) AS num --去重
				FROM
					fdm.fdm_eclp_stock1_saleable_warehouse_stock_chain
				WHERE
					dp = 'ACTIVE'
					AND yn = 1
					AND seller_no in
					(
						select seller_no from app.app_basic_dashboard_goods_seller
					)
			)
			s1
		WHERE
			s1.num = 1
	)
	c
left join fdm.fdm_log_scm_ka_allot_sys_pre_allot d --參數表
on
	c.dept_no = d.dept_no
	and c.warehouse_no = d.warehouse_no
	and c.goods_no = d.goods_id
	and d.yn = 1
	and d.dt = sysdate( - 1)
left join
	(
		select
			*
		from
			fdm.fdm_log_scm_ka_allot_allot_plan --調撥計劃表 (注意去重)
		where
			id in
			(
				select
					max(e.id)
				from
					fdm.fdm_log_scm_ka_allot_allot_plan e
				where
					e.dt = sysdate( - 1)
				group by
					e.dept_name,
					e.in_warehouse_no,
					e.goods_no
			)
			and dt = sysdate( - 1)
	)
	f 
on
	f.dept_no = c.dept_no
	and f.in_warehouse_no = c.warehouse_no
	and f.goods_no = c.goods_no
left join app.app_ka_predict_sales_da g ---線下門店銷量預測表
on
	c.dept_no = g.dept_no
	and c.goods_no = g.goods_no
	and c.warehouse_no = g.warehouse_no
	and g.dt = sysdate( - 1)
left join dim.dim_wms_store h --庫房維表,取出入倉所屬城市
on
	c.warehouse_name = h.dim_store_name
left join dim.dim_wms_store i --庫房維表,取出倉所屬城市
on
	f.out_warehouse_name = i.dim_store_name
left join
	(
		select
			startcityid,
			startcityname,
			endcityid,
			endcityname,
			totalaging,
			totaltime,
			yn
		from
			(
				select
					case
						when
							(
								startprovinceid in(1, 2, 3, 4)
							)
						then startprovinceid
						else startcityid
					end as startcityid,
					case
						when
							(
								startprovinceid in(1, 2, 3, 4)
							)
						then concat(startprovincename, '市')
						else startcityname
					end as startcityname,
					case
						when
							(
								endprovinceid in(1, 2, 3, 4)
							)
						then endprovinceid
						else endcityid
					end as endcityid,
					case
						when
							(
								endprovinceid in(1, 2, 3, 4)
							)
						then concat(endprovincename, '市')
						else endcityname
					end as endcityname,
					ceil(avg(totalaging)) as totalaging,
					ceil(avg(totaltime)) as totaltime,
					max(1) as yn
				from
					fdm.fdm_staticroutebatchgenerate_staticroutebatchgenerate_chain
				where
					dp = 'ACTIVE'
					and yn = 1
				group by
					case
						when
							(
								startprovinceid in(1, 2, 3, 4)
							)
						then startprovinceid
						else startcityid
					end,
					case
						when
							(
								startprovinceid in(1, 2, 3, 4)
							)
						then concat(startprovincename, '市')
						else startcityname
					end,
					case
						when
							(
								endprovinceid in(1, 2, 3, 4)
							)
						then endprovinceid
						else endcityid
					end,
					case
						when
							(
								endprovinceid in(1, 2, 3, 4)
							)
						then concat(endprovincename, '市')
						else endcityname
					end
			)
			route
	)
	j --vlt (青龍路由,經過城市名稱,獲取vlt)
on
	h.loc_city_name = j.startcityname
	and i.loc_city_name = j.endcityname
left join
	(
		select
			*
		from
			(
				SELECT
					seller_id,
					warehouse_id,
					goods_id,
					avg_year_sellnum,
					dt,
					row_number() over(partition by goods_id, seller_id, warehouse_id ORDER BY avg_year_sellnum desc) AS num
				FROM
					app.app_basic_dashboard_goods_avg_year_sellnum --KA商品銷量年日均值(去重)
				WHERE
					dt = sysdate( - 1)
			)
			s2
		where
			s2.num = 1
	)
	m --獲取銷量年日均值
on
	c.seller_id = m.seller_id
	and c.warehouse_id = m.warehouse_id
	and c.goods_id = m.goods_id
	and m.dt = sysdate( - 1)
join app.app_basic_dashboard_goods_seller n
on
	c.seller_id = n.seller_id
             
"""

ht.exec_sql(schema_name = 'app', table_name = 'app_basic_dashboard_goods_sale_predict_daily_tem', sql = sql1, merge_flag = True) 

 四、KA接口表blog

#!/usr/bin/env python3
################################################################
# AUTHOR:         wn
# CREATED TIME:   2018-08-09
# MODIFIED BY:    
# MODIFTED TIME:  
# REVIEWED BY:    
# REVIEWED TIME:  
# COMMENTS: goods

################################################################
#===============================================================================
#  FILE: exe_app_basic_dashboard_goods_sale_predict_daily.py
#  USAGE: ./exe_app_basic_dashboard_goods_sale_predict_daily.py
#  SRC_TABLE: 
#  TGT_TABLE: app.app_basic_dashboard_goods_sale_predict_daily
#===============================================================================
import sys
import os
import time
import datetime
import logging 
import calendar

sys.path.append(os.getenv('HIVE_TASK'))
from HiveTask import HiveTask
ht = HiveTask()
today = ht.oneday(1)[0:10]
yesterday  = ht.oneday(0)[0:10] 


sql1 = """
use app;
	insert overwrite table app.app_basic_dashboard_goods_sale_predict_daily partition
	(
	   dt = '"""+yesterday+"""'
	) 
	select
	k.seller_id, --商家編號
	k.seller_name, --商家名稱
	k.dept_id, --事業部id
	k.dept_no, --事業部編號
	k.dept_name, --事業部名稱
	k.goods_id, --商品id
	k.goods_no, --商品編號
	k.goods_name, --商品名稱
	k.warehouse_id, --入倉id
	k.warehouse_no, --入倉編號
	k.warehouse_name, --入倉名稱
	k.in_warehouse_city, --入倉城市
	k.satisfy_alpha, -- 出倉服務水平c (basis)
	k.safe_stock_days, -- 安全庫存天數(basis)
	k.alt, -- 出倉alt(小時)(basis)
	k.vlt, --運輸時間
	case
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			>= 0
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 1
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) * m.predict_sales_1d)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 1
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 2
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.predict_sales_1d + m.predict_sales_2d) / 2)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 2
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 3
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.predict_sales_1d + m.predict_sales_2d + m.predict_sales_3d) / 3)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 3
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 4
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.predict_sales_1d + m.predict_sales_2d + m.predict_sales_3d + m.predict_sales_4d) / 4)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 4
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 5
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.predict_sales_1d + m.predict_sales_2d + m.predict_sales_3d + m.predict_sales_4d + m.predict_sales_5d) / 5)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 5
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 6
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.predict_sales_1d + m.predict_sales_2d + m.predict_sales_3d + m.predict_sales_4d + m.predict_sales_5d + m.predict_sales_6d) / 6)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 6
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 7
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.predict_sales_1d + m.predict_sales_2d + m.predict_sales_3d + m.predict_sales_4d + m.predict_sales_5d + m.predict_sales_6d + m.predict_sales_7d) / 7)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 7
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			<= 14
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.in_stock_predict_sales_14d) / 14)
		when k.in_stock_safety_num is null
			and
			(
				m.safe_stock_days + m.alt / 24 + m.vlt / 24
			)
			> 14
		then round(m.satisfy_alpha *(m.safe_stock_days + m.alt / 24 + m.vlt / 24) *(m.in_stock_predict_sales_28d) / 28)
		else round(k.in_stock_safety_num)
	end as in_stock_safety_num, --安全庫存(複雜公式見prd)
	k.target_stock_days, -- 目標庫存天數
	k.BP, -- BP
	k.out_warehouse_no, --配出倉編碼
	k.out_warehouse_name, --配出倉名稱
	k.out_warehouse_city, --出倉城市
	case
		when k.in_stock_max_num is null
		then round(m.satisfy_alpha * m.target_stock_days * m.in_stock_predict_sales_14d / 14 + m.BP) --目標庫存天數10,因此沒有判斷條件
		else round(k.in_stock_max_num)
	end as in_stock_max_num, --目標庫存
	k.bef_sales_1d, --T-1日銷量(逆向)
	k.bef_sales_2d, --T-2日銷量
	k.bef_sales_3d, --T-3日銷量
	k.bef_sales_4d, --T-4日銷量
	k.bef_sales_5d, --T-5日銷量
	k.bef_sales_6d, --T-6日銷量
	k.bef_sales_7d, --T-7日銷量
	k.in_stock_sales_14d, --14日銷量
	k.in_stock_sales_28d, --28日銷量
	k.predict_sales_1d, --T+1日預測銷量(正向)
	k.predict_sales_2d, --T+2日預測銷量
	k.predict_sales_3d, --T+3日預測銷量
	k.predict_sales_4d, --T+4日預測銷量
	k.predict_sales_5d, --T+5日預測銷量
	k.predict_sales_6d, --T+6日預測銷量
	k.predict_sales_7d, --T+7日預測銷量
	k.in_stock_predict_sales_14d, --14日預測銷量
	k.in_stock_predict_sales_28d, --28日預測銷量
	1 AS yn, --刪除標識 1爲沒刪,0爲刪除
	'plumber' AS create_pin, --建立人
	'plumber' AS update_pin, --更新人
	current_timestamp AS create_time, --建立時間
	current_timestamp AS update_time, --更新時間
	current_timestamp AS ts --時間戳
from
	(
		select
			*
		from
			app.app_basic_dashboard_goods_sale_predict_daily_tem
		where
			dt = sysdate( - 1)
	)
	k
join
	(
		select
			*
		from
			app.app_basic_dashboard_goods_sale_predict_daily_tem
		where
			dt = sysdate( - 1)
	)
	m
on
	k.dept_no = m.dept_no
	and k.goods_no = m.goods_no
	and k.warehouse_no = m.warehouse_no      
             
"""

ht.exec_sql(schema_name = 'app', table_name = 'app_basic_dashboard_goods_sale_predict_daily', sql = sql1, merge_flag = True) 
相關文章
相關標籤/搜索