add workflow IIRTNC数据刷新,dev

This commit is contained in:
root 2026-02-28 15:29:55 +08:00
parent 668fd057e4
commit 2747e86030
9 changed files with 1649 additions and 0 deletions

View File

@ -0,0 +1,146 @@
#!/usr/bin/python
# -*- encoding=utf-8 -*-
from airflow import DAG
from datetime import datetime, timedelta
from airflow.contrib.hooks.ssh_hook import SSHHook
from airflow.contrib.operators.ssh_operator import SSHOperator
from airflow.sensors.external_task_sensor import ExternalTaskSensor
import json
from airflow.operators.trigger_dagrun import TriggerDagRunOperator
from airflow.operators.email_operator import EmailOperator
from airflow.utils.trigger_rule import TriggerRule
sshHook = SSHHook(ssh_conn_id ='ssh_air')
default_args = {
'owner': 'tek_newsletter@163.com',
'email_on_failure': True,
'email_on_retry':True,
'start_date': datetime(2024, 1, 1),
'depends_on_past': False,
'retries': 6,
'retry_delay': timedelta(minutes=10),
}
dag = DAG('wf_dag_iirtnc_data', default_args=default_args,
schedule_interval="0 20 * * 5",
catchup=False,
dagrun_timeout=timedelta(minutes=600),
max_active_runs=3,
tags=['64cbd1bbced14209b5e3a879f89e8ab1','TK_Cust','IIRTNC数据刷新']
)
task_failed = EmailOperator (
dag=dag,
trigger_rule=TriggerRule.ONE_FAILED,
task_id="task_failed",
to=["tek_newsletter@163.com"],
cc=[""],
subject="iirtnc_data_failed",
html_content='<h3>您好iirtnc_data作业失败请及时处理" </h3>')
dysql_iirtnc_cust_vis = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_cust_vis',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_cust_vis"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_white_list = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_white_list',
command='/data/airflow/etl/COM/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_white_list"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_iirtnc_leads = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_leads',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_leads"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_iirtnc_order = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_order',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_order"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_iirtnc_pos = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_pos',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_pos"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_iirtnc_new_funnel = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_new_funnel',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_new_funnel"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_iirtnc_demo = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_demo',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_demo"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_iirtnc_funnel_won = SSHOperator(
ssh_hook=sshHook,
task_id='dysql_iirtnc_funnel_won',
command='/data/airflow/etl/MART/run_psql.sh {{ ds_nodash }} {{params.my_param}} ',
params={'my_param':"dysql_iirtnc_funnel_won"},
depends_on_past=False,
retries=3,
dag=dag)
dysql_white_list >> dysql_iirtnc_new_funnel
dysql_white_list >> dysql_iirtnc_pos
dysql_white_list >> dysql_iirtnc_leads
dysql_white_list >> dysql_iirtnc_order
dysql_white_list >> dysql_iirtnc_funnel_won
dysql_white_list >> dysql_iirtnc_demo
dysql_white_list >> dysql_iirtnc_cust_vis
dysql_iirtnc_cust_vis >> task_failed

View File

@ -0,0 +1,95 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='Customer Visibility' and date_id>=current_date;
;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,value2,application
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
demo_type,
region region_name,
'Customer Visibility',
(count(distinct p1.company)-1),
count(distinct p1.account_name),
sub_application,
'com' src_sysname,
'white_list' src_table,
'tk_customer_visibility_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (
select distinct p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p5.region,'' demo_type,p5.account_name,coalesce(p1.company,'') company from p30_common.cust_company_info p1
right join p30_common.white_list p5
on p1.company =p5.account_name
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p5.as_of_date =p2.standard_dt
)p1
where as_of_date>=current_date
group by 1,2,3,4,5,6,7,8,9,10,14;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='Customer Visibility' and date_id>=current_date;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,value2,application ,account_name,account_manager
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
demo_type,
region region_name,
'Customer Visibility',
(count(distinct p1.company)-1),
count(distinct p1.account_name),
sub_application,
account_name,
account_manager,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_demo_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p5.region,'' demo_type,p5.account_name,coalesce(p1.company,'') company,coalesce(p6.salers,'') account_manager from p30_common.cust_company_info p1
right join p30_common.white_list p5
on p1.company =p5.account_name
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p5.as_of_date =p2.standard_dt
left join p30_common.d_account_info p6
on p5.account_name=p6.name)p1
where as_of_date>=current_date
group by 1,2,3,4,5,6,7,8,9,10,15,14,16;
\q

View File

@ -0,0 +1,148 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
drop table if exists tk_demo_t;
create temporary table tk_demo_t as
select 'JDY' data_source,coalesce(p3.sales_teams,assign_id) account_manager,p1.visit_time::date visit_date,p2.account_name,'' dealer_name,'Onsite' demo_type,'' market_area from p20_pdm.t01_jdy_daily_visit p1
left join p20_pdm.t01_jdy_account p2
on case when p1.account <>'' then p1.account else p1.account2 end
=case when p2.account_nick_name <> '' then p2.account_nick_name else p2.account_name end
inner join p31_dim.d_sales_mapping p3
on p1.assign_id=p3.jdy_wechat
where p2.account_nick_name is not null
union all
select 'Partner',coalesce(p3.sales_teams,''),insert_date,customer_name ,dealer_name,case when contact_ways in ('电话沟通','微信联络') then 'Remote' else 'Onsite' end demo_type,case when p1.area in ('华南','华东') then 'SE' when p1.area in ('华北') then 'NW' else p1.area end area
from p20_pdm.t01_partner_visit p1
left join p20_pdm.t00_china_city_pnum_ai p5
on p1.contact_province=p5.prov_name
and p5.city_name like substring(p1.contact_city,1,2)||'%'
and coalesce(substring(p5.district,1,2),'')=substring(p1.contact_district,1,2)
left join p31_dim.d_sales_mapping p3
on p5.saler_name=p3.sales_dw
;
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='Demo' and date_id>='2025-01-01';
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,type2
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
demo_type,
market_area region_name,
'Demo',
count(*),
sub_application,
data_source,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_demo_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,p5.industry,coalesce(p5.sub_application,'') sub_application,p5.sub_industry,p1.demo_type,p1.account_name,p1.market_area,p1.data_source from tk_demo_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.visit_date =p2.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%')p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='Demo' and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager,type1,type2
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
demo_type,
market_area region_name,
'Demo',
count(*),
sub_application,
account_name,
account_manager,
dealer_name,
data_source,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_demo_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,p5.industry,coalesce(p5.sub_application,'') sub_application,p1.dealer_name,p5.sub_industry,p1.demo_type,p1.account_name,p1.market_area,p1.account_manager,p1.data_source,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_demo_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.visit_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18;
delete from p60_mart.iirtnc_demo_detail;
insert into p60_mart.iirtnc_demo_detail
SELECT insert_date,'Partner', id, visitor, dealer_name, customer_name, intention_product,coalesce(p3.sales_teams,'') account_manager, contact_ways,case when p1.area = '华南' then 'SE' else 'NW' end market_area, p2.industry, p2.sub_industry, p1.etl_batch_no, p1.etl_first_dt, p1.etl_job, p1.etl_proc_dt, p1.etl_tx_dt, p1.src_sysname, p1.src_table,case when p2.account_name is not null then 1 else 0 end white_list_ind,p2.sub_application
FROM p20_pdm.t01_partner_visit p1
left join p30_common.white_list p2
--on p2.tk_week_end_date=p5.as_of_date::date
on p2.as_of_date::date='2026-01-09'
and p1.dealer_name like '%'||p2.keys||'%'
left join p20_pdm.t00_china_city_pnum_ai p5
on p1.contact_province=p5.prov_name
and p5.city_name like substring(p1.contact_city,1,2)||'%'
and coalesce(substring(p5.district,1,2),'')=substring(p1.contact_district,1,2)
left join p31_dim.d_sales_mapping p3
on p5.saler_name=p3.sales_dw
;
insert into p60_mart.iirtnc_demo_detail
SELECT visit_time,'JDY', id, contact_name, '', account,product_family1 intention_product,coalesce(p3.sales_teams,p1.assign_id) account_manager,'' contact_ways,'' market_area, p2.industry, p2.sub_industry, p1.etl_batch_no, p1.etl_first_dt, p1.etl_job, p1.etl_proc_dt, p1.etl_tx_dt, p1.src_sysname, p1.src_table,case when p2.account_name is not null then 1 else 0 end white_list_ind,p2.sub_application
FROM p20_pdm.t01_jdy_daily_visit p1
left join p30_common.white_list p2
--on p2.tk_week_end_date=p5.as_of_date::date
on p2.as_of_date::date='2026-01-09'
and p1.account like '%'||p2.keys||'%'
inner join p31_dim.d_sales_mapping p3
on p1.assign_id=p3.jdy_wechat
;
\q

View File

@ -0,0 +1,169 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
drop table if exists tk_funnel_won_t;
create temporary table tk_funnel_won_t
as
select distinct p1.record_id ,'Partner' source_channel,coalesce(p3.sales_teams,p1.check_admin_name) account_manager, complete_date::date ,case when region_name in ('华南','华东') then 'SE' when region_name in ('华北') then 'NW' else region_name end region_name
,customer_name ,rp_total_price*p2.target_currency_value /p2.currency_value as total_amt from p20_pdm.t01_partner_report p1
left join p31_dim.d_sales_mapping p3
on p1.check_admin_name=p3.sales_one_site
left join p31_dim.d_currency p2
on p2.currency_cd ='CNY'
and p2.target_currency_cd ='USD'
where progress ='100%' and complete_date >='2025-01-01'
union all
select distinct p1.tek_opportunity_id ,'CRM' source_channel ,coalesce(p2.sales_teams,p1.acount_manager) account_manager,p1.actual_close_time::date,case when upper(p1.area_id) like '%SOUTH%' then 'SE' when upper(p1.area_id) like '%NORTH%' then 'NW' else p1.area_id end area_id
,p1.parent_account_name account_name,
case when p1.discount_amount=0 and p1.channel_id='Distribution' then p1.total_amount*0.55/p4.currency_value*p4.target_currency_value else p1.total_amount/p4.currency_value*p4.target_currency_value end total_amount
from p20_pdm.t01_crm_opportunity p1
left join p31_dim.d_sales_mapping p2
on p1.acount_manager=p2.sales_teams
left join p31_dim.d_currency p4
on p1.currency_name=p4.currency_name
and p4.target_currency_cd ='USD'
where p1.state in ('Won','赢单')
--and (p1.product_class like '2025%' or p1.product_class like '2035%' or p1.product_class like '2610%' or p1.product_class like '2026%' or p1.product_class like '2036%')
and p1.actual_close_time >= '2025-01-01';
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='Funnel Won' and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,type1
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
region_name,
'Funnel Won',
sum(total_amt),
sub_application,
source_channel,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_funnel_won' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.record_id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p1.source_channel,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p1.region_name,p1.customer_name,p1.total_amt from tk_funnel_won_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.complete_date =p2.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p5.keys||'%')p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='Funnel Won' and date_id>='2025-01-01';
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,type1, account_name,account_manager
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
region_name,
'Funnel Won',
sum(total_amt),
sub_application,
source_channel,
account_name,
account_manager,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_funnel_won' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.record_id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p1.source_channel,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p1.region_name,p1.customer_name account_name,p1.total_amt,p1.account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_funnel_won_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.complete_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p5.keys||'%')p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14,15,16,17;
delete from p60_mart.iirtnc_funnel_won_detail;
insert into p60_mart.iirtnc_funnel_won_detail
SELECT complete_date::date,'Partner' source_channel,record_id, report_num, dealer_name
, coalesce(p3.sales_teams,p1.apply_admin_name), customer_name
, total_price*p2.target_currency_value /p2.currency_value total_price
, series_name, product_type, product_sku_type, product_name, skus
,list_price*p2.target_currency_value /p2.currency_value list_price, count count_qty
,rp_total_price*p2.target_currency_value /p2.currency_value rp_total_price
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
FROM p20_pdm.t01_partner_report p1
left join p31_dim.d_sales_mapping p3
on p1.check_admin_name=p3.sales_one_site
left join p31_dim.d_currency p2
on p2.currency_cd ='CNY'
and p2.target_currency_cd ='USD'
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p5.keys||'%'
where progress ='100%' and complete_date >='2025-01-01'
union all
SELECT actual_close_time,'CRM' source_channel ,tek_opportunity_id, opportunity_name,parent_account_name
,coalesce(p2.sales_teams,p1.acount_manager) ,parent_account_name
,p1.base_amount/p4.currency_value*p4.target_currency_value base_amount
,split_part(product_class, ':', 2) product_class ,split_part(product_class, ':', 1),'' ,product_name ,'' sku
,p1.base_amount/p4.currency_value*p4.target_currency_value base_amount,case when uint_price =0 then 1 else base_amount/uint_price end count_qty
,p1.total_amount/p4.currency_value*p4.target_currency_value total_amount
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
FROM p20_pdm.t01_crm_opportunity p1
left join p31_dim.d_sales_mapping p2
on p1.acount_manager=p2.sales_teams
left join p20_pdm.t01_crm_contact p3
on p1.crm_contact_number =p3.crm_contact_account
left join p31_dim.d_currency p4
on p1.currency_name=p4.currency_name
and p4.target_currency_cd ='USD'
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.parent_account_name like '%'||p5.keys||'%'
where p1.state in ('Won','赢单')
and (p1.product_class like '2025%' or p1.product_class like '2035%' or p1.product_class like '2610%' or p1.product_class like '2036%' or p1.product_class like '2026%' );
\q

View File

@ -0,0 +1,124 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
drop table if exists tk_raw_t;
create temporary table tk_raw_t
as
select company_name account_name,create_time::date as_of_date,coalesce(p2.sales_teams,p1.account_manager) account_manager,lead_number ,case when tek_assigntopartnercompanyidname <>'' then 1 else 0 end assign_partner,case when channel_type='Distribution' then 'Distribution' else 'TSM' end channel_type,1 leads_qty
from p20_pdm.t01_crm_raw_leads p1
left join p31_dim.d_sales_mapping p2
on p1.account_manager=p2.sales_teams
where qualification_group in ('Sales Qualification','PIAM Qualification','Partner Qualification');
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='Leads'
and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,value2,type1
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'Leads',
sum(assign_partner),
sub_application,
sum(leads_qty),
channel_type,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.lead_number) p2.tk_week_end_date as_of_date,p2.tk_year,p1.channel_type,p5.sub_application,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,p5.industry,p5.sub_industry,p5.region area_id,p1.account_name,p1.assign_partner,p1.leads_qty from tk_raw_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%')p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,15;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='Leads' and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,value2,type1, account_name,account_manager
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id region_name,
'Leads',
sum(assign_partner),
sub_application,
sum(leads_qty),
channel_type,
account_name,
account_manager,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_demo_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.lead_number) p2.tk_week_end_date as_of_date,p1.channel_type,p5.sub_application,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,p5.industry,p5.sub_industry,p5.region area_id,p1.account_name,p1.assign_partner,p1.leads_qty,P1.account_manager,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_raw_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,17,16,15,13,18;
delete from p60_mart.iirtnc_leads_detail;
insert into p60_mart.iirtnc_leads_detail
select company_name account_name,create_time::date as_of_date,coalesce(p2.sales_teams,p1.account_manager) account_manager
,lead_number ,case when tek_assigntopartnercompanyidname <>'' then 1 else 0 end assign_partner
,case when channel_type='Distribution' then 'Distribution' else 'TSM' end channel_type,p1.channel_type origin_channel_type
,p1.name contact_name,p1.mobile_phone,p1.email,p1.tek_assigntopartnercompanyidname partner_company
,case when p5.account_name is not null then 1 else 0 end white_list_ind
,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry,coalesce(p5.sub_application,'') application
from p20_pdm.t01_crm_raw_leads p1
left join p31_dim.d_sales_mapping p2
on p1.account_manager=p2.sales_teams
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.company_name like '%'||p5.keys||'%'
where qualification_group in ('Sales Qualification','PIAM Qualification','Partner Qualification');
\q

View File

@ -0,0 +1,167 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
drop table if exists tk_new_funnel_t;
create temporary table tk_new_funnel_t
as
select p1.record_id ,check_date::date ,'Partner' source_channel,coalesce(p3.sales_teams,p1.check_admin_name) account_manager,case when region_name in ('华南','华东') then 'SE' when region_name in ('华北') then 'NW' else region_name end region_name
,customer_name ,rp_total_price*p2.target_currency_value /p2.currency_value as total_amt from p20_pdm.t01_partner_report p1
left join p31_dim.d_sales_mapping p3
on p1.check_admin_name=p3.sales_one_site
left join p31_dim.d_currency p2
on p2.currency_cd ='CNY'
and p2.target_currency_cd ='USD'
where check_status_str ='已通过' and check_date >='2025-01-01'
union all
select p1.tek_opportunity_id ,p1.create_time::date,'CRM' source_channel,coalesce(p2.sales_teams,p1.acount_manager) account_manager,case when upper(p1.area_id) like '%SOUTH%' then 'SE' when upper(p1.area_id) like '%NORTH%' then 'NW' else p1.area_id end area_id,p1.parent_account_name account_name,case when p1.discount_amount=0 and p1.channel_id='Distribution' then p1.base_amount*0.55 else p1.base_amount end total_amount
from p20_pdm.t01_crm_opportunity p1
left join p31_dim.d_sales_mapping p2
on p1.acount_manager=p2.sales_teams
left join p31_dim.d_currency p4
on p1.currency_name=p4.currency_name
and p4.target_currency_cd ='USD'
where
--(p1.product_class like '2025%' or p1.product_class like '2035%' or p1.product_class like '2610%' or p1.product_class like '2026%' or p1.product_class like '2036%')
--and
p1.create_time >= '2025-01-01';
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='New Funnel' and date_id>='2025-01-01';
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,type1
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
region_name,
'New Funnel',
sum(total_amt),
sub_application,
source_channel,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_new_funnel' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.record_id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p1.source_channel,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p1.region_name,p1.customer_name,p1.total_amt from tk_new_funnel_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.check_date =p2.standard_dt
inner join (select distinct on (as_of_date,keys) * from p30_common.white_list) p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p5.keys||'%' )p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='New Funnel' and date_id>='2025-01-01';
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,type1, account_name,account_manager
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
region_name,
'New Funnel',
sum(total_amt),
sub_application,
source_channel,
account_name,
account_manager,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_funnel_won' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.record_id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p1.source_channel,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p1.region_name,p1.customer_name account_name,p1.total_amt,p1.account_manager account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_new_funnel_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.check_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p5.keys||'%')p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14,15,16,17;
delete from p60_mart.iirtnc_new_funnel_detail;
insert into p60_mart.iirtnc_new_funnel_detail
SELECT p1.check_date::date,'Partner' source_channel,record_id, report_num, dealer_name
, coalesce(p3.sales_teams,p1.apply_admin_name), customer_name
, total_price*p2.target_currency_value /p2.currency_value total_price
, series_name, product_type, product_sku_type, product_name, skus
,list_price*p2.target_currency_value /p2.currency_value list_price, count count_qty
,rp_total_price*p2.target_currency_value /p2.currency_value rp_total_price
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
FROM p20_pdm.t01_partner_report p1
left join p31_dim.d_sales_mapping p3
on p1.check_admin_name=p3.sales_one_site
left join p31_dim.d_currency p2
on p2.currency_cd ='CNY'
and p2.target_currency_cd ='USD'
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p5.keys||'%'
where check_status_str ='已通过'
union all
SELECT p1.create_time,'CRM' source_channel ,tek_opportunity_id, opportunity_name,parent_account_name
,coalesce(p2.sales_teams,p1.acount_manager) ,parent_account_name
,p1.base_amount/p4.currency_value*p4.target_currency_value base_amount
,split_part(product_class, ':', 2) product_class ,split_part(product_class, ':', 1),'' ,product_name ,'' sku
,p1.base_amount/p4.currency_value*p4.target_currency_value base_amount,case when uint_price =0 then 1 else base_amount/uint_price end count_qty
,p1.total_amount/p4.currency_value*p4.target_currency_value total_amount
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
FROM p20_pdm.t01_crm_opportunity p1
left join p31_dim.d_sales_mapping p2
on p1.acount_manager=p2.sales_teams
left join p20_pdm.t01_crm_contact p3
on p1.crm_contact_number =p3.crm_contact_account
left join p31_dim.d_currency p4
on p1.currency_name=p4.currency_name
and p4.target_currency_cd ='USD'
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.parent_account_name like '%'||p5.keys||'%'
--where (p1.product_class like '2025%' or p1.product_class like '2035%' or p1.product_class like '2610%' or p1.product_class like '2026%' or p1.product_class like '2036%');
;
\q

View File

@ -0,0 +1,322 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
drop table if exists tk_order_t;
create temporary table tk_order_t
as
select distinct on (salesorderdetailid) salesorderdetailid orderbumber,tek_orderbookdate::date as_of_date ,tek_productfamilycode,coalesce(p2.sales_teams,p1.tek_accountmanagerid) account_manager,tek_contactcompanyname account_name,tek_productfamilycode product_type,tek_productgroup3name product_name,extendedamount_base total_amount
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
--inner join p31_dim.d_product_family_code p3
--on p1.tek_productfamilycode=p3.product_family_code
--and p3.category ='High Touch'
where p1.tek_orderstatus in ('Booked','Shipped')
and tek_orderbookdate::date>='2025-01-01'
;
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='Order' and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'Order',
sum(total_amount),
sub_application,
'com' src_sysname,
'crm_order' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.orderbumber) p5.keys,p5.account_name,p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,p5.industry,p5.sub_industry,coalesce(p5.sub_application,'') sub_application,COALESCE(P5.REGION,'') area_id,p1.account_name,p1.total_amount from tk_order_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date::date =p2.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
inner join p31_dim.d_product_family_code p3
on (p1.tek_productfamilycode=p3.product_family_code
and p3.category ='High Touch'
and (lower(p5.industry)='auto'or (p5.sub_application is not null)))
)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'Order',
sum(total_amount),
sub_application,
'com' src_sysname,
'crm_order' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.orderbumber) p5.keys,p5.account_name,p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,p5.industry,p5.sub_industry,coalesce(p5.sub_application,'') sub_application,COALESCE(P5.REGION,'') area_id,p1.account_name,p1.total_amount from tk_order_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date::date =p2.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
where (lower(p5.industry)<>'auto'and (p5.sub_application is null))
)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='Order' and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'Order',
sum(total_amount),
sub_application,
account_name,
account_manager,
white_list_ind,
'com' src_sysname,
'crm_order' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.orderbumber) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p5.region area_id,p1.account_name,p1.total_amount,p1.account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind
from tk_order_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date::date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
inner join p31_dim.d_product_family_code p3
on (p1.tek_productfamilycode=p3.product_family_code
and p3.category ='High Touch'
and (lower(p5.industry)='auto'or (p5.sub_application is not null)))
)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,14,13,15,16;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'Order',
sum(total_amount),
sub_application,
account_name,
account_manager,
white_list_ind,
'com' src_sysname,
'crm_order' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.orderbumber) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p5.region area_id,p1.account_name,p1.total_amount,p1.account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_order_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date::date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
where (lower(p5.industry)<>'auto'and (p5.sub_application is null)))p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,14,13,15,16;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'Order',
sum(total_amount),
sub_application,
account_name,
account_manager,
white_list_ind,
'com' src_sysname,
'crm_order' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.orderbumber) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p5.region area_id,p1.account_name,p1.total_amount,p1.account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_order_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date::date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
where p5.account_name is null)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,14,13,15,16;
delete from p60_mart.iirtnc_order_detail;
insert into p60_mart.iirtnc_order_detail
select distinct on (salesorderdetailid) p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,salesorderdetailid orderbumber,tek_orderbookdate::date order_date ,tek_contactcompanyname account_name,tek_productfamilycode product_type,tek_productgroup3name product_name,extendedamount_base total_amount
,coalesce(p2.sales_teams,p1.tek_accountmanagerid) account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
inner join p31_dim.d_product_family_code p4
on p1.tek_productfamilycode=p4.product_family_code
and p4.category ='High Touch'
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.tek_orderbookdate::date =p3.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.tek_contactcompanyname like '%'||p5.keys||'%'
where (lower(p5.industry)='auto' or (p5.sub_application is not null))
;
insert into p60_mart.iirtnc_order_detail
select distinct on (salesorderdetailid) p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,salesorderdetailid orderbumber,tek_orderbookdate::date order_date ,tek_contactcompanyname account_name,tek_productfamilycode product_type,tek_productgroup3name product_name,extendedamount_base total_amount
,coalesce(p2.sales_teams,p1.tek_accountmanagerid) account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.tek_orderbookdate::date =p3.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.tek_contactcompanyname like '%'||p5.keys||'%'
where (lower(p5.industry)<>'auto'and (p5.sub_application is null))
;
insert into p60_mart.iirtnc_order_detail
select distinct on (salesorderdetailid) p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,salesorderdetailid orderbumber,tek_orderbookdate::date order_date ,tek_contactcompanyname account_name,tek_productfamilycode product_type,tek_productgroup3name product_name,extendedamount_base total_amount
,coalesce(p2.sales_teams,p1.tek_accountmanagerid) account_manager
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.tek_orderbookdate::date =p3.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.tek_contactcompanyname like '%'||p5.keys||'%'
where p5.account_name is null
;
\q

View File

@ -0,0 +1,415 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
drop table if exists tk_pos_t;
create temporary table tk_pos_t
as
select id,'Partner' source_channel,pos_insert_date as_of_date,coalesce(p4.sales_teams,'') account_manager,product_family_code,case when area_name in ('华南','华东') then 'SE' when area_name in ('华北') then 'NW' else area_name end area_ID,distributor_name2 dealer_name,customer_name account_name,extended_price *p2.target_currency_value /p2.currency_value total_amount from p20_pdm.t01_partner_pos p1
left join p31_dim.d_currency p2
on p2.currency_cd =p1.sales_trx_currency_code
and p2.target_currency_cd ='USD'
left join p20_pdm.t00_china_city_pnum_ai p3
on p1.province=p3.prov_name
and case when coalesce(p3.district,'') <>'' then p3.district
else p3.city_name end like substring(p1.city,1,2)||'%'
left join p31_dim.d_sales_mapping p4
on p4.sales_dw=p3.saler_name
--inner join p31_dim.d_product_family_code p4
--on p1.product_family_code=p4.product_family_code
--and p4.category ='Low Touch'
where extended_price <>0
;
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='POS'
and date_id>='2025-01-01'
;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'POS',
sum(total_amount),
sub_application,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p1.area_id,p1.account_name,p1.total_amount from tk_pos_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
inner join p31_dim.d_product_family_code p4
on p1.product_family_code=p4.product_family_code
and p4.category ='Low Touch'
and (lower(p5.industry)='auto'or (p5.sub_application is not null)))p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'POS',
sum(total_amount),
sub_application,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,p1.area_id,p1.account_name,p1.total_amount from tk_pos_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
where (lower(p5.industry)<>'auto' and (p5.sub_application is null)))p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13;
delete from p60_mart.iirtnc_dashboard_data_detail
where ind_cd='POS' and date_id>='2025-01-01';
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager,type1
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'POS',
sum(total_amount),
sub_application,
account_name,
account_manager,
dealer_name,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p1.dealer_name,p5.industry,p5.sub_industry,p1.area_id,p1.account_name,p1.total_amount,p1.account_manager,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_pos_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
where (lower(p5.industry)<>'auto' and (p5.sub_application is null)))p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14,15,16,17;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager,type1
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'POS',
sum(total_amount),
sub_application,
account_name,
account_manager,
dealer_name,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p1.dealer_name,p5.industry,p5.sub_industry,p1.area_id,p1.account_name,p1.total_amount,p1.account_manager,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_pos_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
inner join p31_dim.d_product_family_code p4
on p1.product_family_code=p4.product_family_code
and p4.category ='Low Touch'
and (lower(p5.industry)='auto'or (p5.sub_application is not null)))p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14,15,16,17;
INSERT INTO p60_mart.iirtnc_dashboard_data_detail
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application, account_name,account_manager,type1
,white_list_ind, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
'' demo_type,
area_id,
'POS',
sum(total_amount),
sub_application,
account_name,
account_manager,
dealer_name,
white_list_ind,
'com' src_sysname,
'crm_opportunity_weekly' src_table,
'tk_pos_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2025-01-01' etl_tx_dt,
'0' etl_batch_no
from (select distinct on (p1.id) p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p1.dealer_name,p5.industry,p5.sub_industry,p1.area_id,p1.account_name,p1.total_amount,p1.account_manager,case when p5.account_name is not null then 1 else 0 end white_list_ind from tk_pos_t p1
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p2
on p1.as_of_date =p2.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.account_name like '%'||p5.keys||'%'
where p5.account_name is null)p1
where as_of_date>='2025-01-01'
group by 1,2,3,4,5,6,7,8,9,10,13,14,15,16,17;
delete from p60_mart.iirtnc_pos_detail;
insert into p60_mart.iirtnc_pos_detail
select p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,id tek_opportunity_id
,coalesce(p7.sales_teams,'') account_manager,pos_insert_date status_last_update
,distributor_name parent_account_name ,customer_name name,'' product_series,p1.product_family_code product_type,product_family_name product_name
,1 count_qty,'Partner' source_channel,case when area_name in ('华南','华东') then 'SE' when area_name in ('华北') then 'NW' else area_name end area_id
,customer_name account_name,extended_price *p2.target_currency_value /p2.currency_value total_amount
,case when p6.account_name is not null then 1 else 0 end white_list_ind,coalesce(p6.sub_application,'') sub_application,coalesce(p6.industry,'') industry,coalesce(p6.sub_industry) sub_industry
from p20_pdm.t01_partner_pos p1
left join p31_dim.d_currency p2
on p2.currency_cd =p1.sales_trx_currency_code
and p2.target_currency_cd ='USD'
inner join p30_common.white_list p6
--on p2.tk_week_end_date=p5.as_of_date::date
on p6.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p6.keys||'%'
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.pos_insert_date =p3.standard_dt
left join p20_pdm.t00_china_city_pnum_ai p5
on p1.province=p5.prov_name
and case when coalesce(p5.district,'') <>'' then p5.district
else p5.city_name end like substring(p1.city,1,2)||'%'
left join p31_dim.d_sales_mapping p7
on p5.saler_name=p7.sales_dw
inner join p31_dim.d_product_family_code p4
on p1.product_family_code=p4.product_family_code
and p4.category ='Low Touch'
where extended_price <>0 and (lower(p6.industry)='auto'or (p6.sub_application is not null))
union all
select p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,id tek_opportunity_id
,coalesce(p7.sales_teams,'') account_manager,pos_insert_date status_last_update
,distributor_name parent_account_name ,customer_name name,'' product_series,p1.product_family_code product_type,product_family_name product_name
,1 count_qty,'Partner' source_channel,case when area_name in ('华南','华东') then 'SE' when area_name in ('华北') then 'NW' else area_name end area_id
,customer_name account_name,extended_price *p2.target_currency_value /p2.currency_value total_amount,case when p6.account_name is not null then 1 else 0 end white_list_ind,coalesce(p6.sub_application,'') sub_application,coalesce(p6.industry,'') industry,coalesce(p6.sub_industry) sub_industry
from p20_pdm.t01_partner_pos p1
left join p31_dim.d_currency p2
on p2.currency_cd =p1.sales_trx_currency_code
and p2.target_currency_cd ='USD'
inner join p30_common.white_list p6
--on p2.tk_week_end_date=p5.as_of_date::date
on p6.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p6.keys||'%'
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.pos_insert_date =p3.standard_dt
left join p20_pdm.t00_china_city_pnum_ai p5
on p1.province=p5.prov_name
and case when coalesce(p5.district,'') <>'' then p5.district
else p5.city_name end like substring(p1.city,1,2)||'%'
left join p31_dim.d_sales_mapping p7
on p5.saler_name=p7.sales_dw
where extended_price <>0 and (lower(p6.industry) <>'auto' and (p6.sub_application is null))
union ALL
select distinct on (salesorderdetailid) p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,salesorderdetailid orderbumber
,coalesce(p2.sales_teams,''),tek_orderbookdate::date order_date
,tek_billtocompanyname,tek_contactcompanyname account_name,'',tek_productfamilycode product_type,tek_productgroup3name product_name
,p1.qty,'Order','',tek_contactcompanyname,extendedamount_base total_amount
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
inner join p31_dim.d_product_family_code p4
on p1.tek_productfamilycode=p4.product_family_code
and p4.category ='High Touch'
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.tek_orderbookdate::date =p3.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.tek_contactcompanyname like '%'||p5.keys||'%'
where (lower(p5.industry)='auto' or (p5.sub_application is not null))
union ALL
select distinct on (salesorderdetailid) p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,salesorderdetailid orderbumber
,coalesce(p2.sales_teams,''),tek_orderbookdate::date order_date
,tek_billtocompanyname,tek_contactcompanyname account_name,'',tek_productfamilycode product_type,tek_productgroup3name product_name
,p1.qty,'Order','',tek_contactcompanyname,extendedamount_base total_amount
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.tek_orderbookdate::date =p3.standard_dt
inner join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.tek_contactcompanyname like '%'||p5.keys||'%'
where (lower(p5.industry)<>'auto'and (p5.sub_application is null))
;
insert into p60_mart.iirtnc_pos_detail
select distinct on (salesorderdetailid) p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,salesorderdetailid orderbumber
,coalesce(p2.sales_teams,''),tek_orderbookdate::date order_date
,tek_billtocompanyname,tek_contactcompanyname account_name,'',tek_productfamilycode product_type,tek_productgroup3name product_name
,p1.qty,'Order','',tek_contactcompanyname,extendedamount_base total_amount
,case when p5.account_name is not null then 1 else 0 end white_list_ind,coalesce(p5.sub_application,'') sub_application,coalesce(p5.industry,'') industry,coalesce(p5.sub_industry,'') sub_industry
from p20_pdm.t01_crm_order p1
left join p31_dim.d_sales_mapping p2
on p1.tek_accountmanagerid=p2.sales_teams
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.tek_orderbookdate::date =p3.standard_dt
left join p30_common.white_list p5
--on p2.tk_week_end_date=p5.as_of_date::date
on p5.as_of_date::date='2026-01-09'
and p1.tek_contactcompanyname like '%'||p5.keys||'%'
where p5.account_name is null
;
insert into p60_mart.iirtnc_pos_detail
select p3.tk_week_end_date as_of_date,p3.tk_year,p3.tk_ap,p3.tk_quarter,p3.tk_ap_week
,p3.tk_month,id tek_opportunity_id
,coalesce(p7.sales_teams,'') account_manager,pos_insert_date status_last_update
,distributor_name parent_account_name ,customer_name name,'' product_series,p1.product_family_code product_type,product_family_name product_name
,1 count_qty,'Partner' source_channel,case when area_name in ('华南','华东') then 'SE' when area_name in ('华北') then 'NW' else area_name end area_id
,customer_name account_name,extended_price *p2.target_currency_value /p2.currency_value total_amount,case when p6.account_name is not null then 1 else 0 end white_list_ind,coalesce(p6.sub_application,'') sub_application,coalesce(p6.industry,'') industry,coalesce(p6.sub_industry) sub_industry
from p20_pdm.t01_partner_pos p1
left join p31_dim.d_currency p2
on p2.currency_cd =p1.sales_trx_currency_code
and p2.target_currency_cd ='USD'
left join p30_common.white_list p6
--on p2.tk_week_end_date=p5.as_of_date::date
on p6.as_of_date::date='2026-01-09'
and p1.customer_name like '%'||p6.keys||'%'
left join (
select p1.tk_ap,p1.standard_dt,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
group by 1,2,3,4,5,6) p3
on p1.pos_insert_date =p3.standard_dt
left join p20_pdm.t00_china_city_pnum_ai p5
on p1.province=p5.prov_name
and case when coalesce(p5.district,'') <>'' then p5.district
else p5.city_name end like substring(p1.city,1,2)||'%'
left join p31_dim.d_sales_mapping p7
on p5.saler_name=p7.sales_dw
where extended_price <>0 and p6.account_name is null;
\q

View File

@ -0,0 +1,63 @@
/********************************************************************************************/
/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
delete FROM p30_common.white_list
where as_of_date=current_date;
insert into p30_common.white_list
SELECT distinct current_date as_of_date,extract_date, account_name, keys, city, district, industry, sub_industry, tags, tsm, region, custom_type, url, market_capitalization, order_last_3y, pos_last_3y, total, open_funnel, contact, financing_round, open_lab_visit
,0 Etl_Batch_No
,current_date Etl_First_Dt
,'white_list' Etl_Job
,current_timestamp(0) Etl_Proc_Dt
,current_date Etl_Tx_Dt
,Substr('white_list',1,3) Src_Sysname
,'white_list' Src_Table
,sub_application
FROM p70_ai_intelligence.t_white_list;
delete from p60_mart.iirtnc_dashboard_data
where ind_cd='White List' and date_id>=date_trunc('month', current_date)::date;
;
INSERT INTO p60_mart.iirtnc_dashboard_data
(date_id,year_id, quarter_id, tk_ap, month_id, week_id,industry,sub_industry,demo_type, market_area, ind_cd, value,application,value2
, src_sysname, src_table, etl_job, etl_first_dt, etl_proc_dt, etl_tx_dt, etl_batch_no)
select
as_of_date,
tk_year,
tk_quarter,
tk_ap,
tk_month,
tk_ap_week,
industry,
sub_industry,
demo_type,
'' region_name,
'White List',
count(distinct p1.account_name),
sub_application,
0,
'com' src_sysname,
'white_list' src_table,
'tk_customer_visibility_t' etl_job,
current_timestamp(0) etl_first_dt,
current_timestamp(0) etl_proc_dt,
'2026-01-09' etl_tx_dt,
'0' etl_batch_no
from (
select distinct p2.tk_week_end_date as_of_date,p2.tk_year,p2.tk_ap,p2.tk_quarter,p2.tk_ap_week,p2.tk_month,coalesce(p5.sub_application,'') sub_application,p5.industry,p5.sub_industry,'' demo_type,p5.account_name from p30_common.white_list p5
inner join (
select p1.tk_ap,p1.tk_year,p1.tk_ap_week,p1.tk_month,p1.tk_quarter,max(p2.standard_dt) tk_week_end_date from p31_dim.d_date p1
left join p31_dim.d_date p2
on p1.tk_year=p2.tk_year
and p1.tk_ap_week=p2.tk_ap_week
where (p1.tk_ap,p1.tk_ap_week) in (select tk_ap,min(tk_ap_week) from p31_dim.d_date group by 1)
group by 1,2,3,4,5) p2
on p5.as_of_date =p2.tk_week_end_date)p1
where as_of_date>=date_trunc('month', current_date)::date
group by 1,2,3,4,5,6,7,8,9,10,13;
\q