基於Python-sqlparse的SQL表血緣追蹤解析實現
select
b.product_name " 產品 ",
count(a.order_id) " 訂單量 ",
b.selling_price_max " 銷售價 ",
b.gross_profit_rate_max/100 " 毛利率 ",
case when b.business_type =1 then ' 自營消化 ' when b.business_type =2 then ' 服務商消化 ' end " 消化模式 "
from(select 'CRM 簽單 ' label,date(d.update_ymd) close_ymd,c.product_name,c.product_id,
a.order_id,cast(a.recipient_amount as double) amt,d.cost
from mysql4.dataview_fenxiao.fx_order a
left join mysql4.dataview_fenxiao.fx_order_task b on a.order_id = b.order_id
left join mysql7.dataview_trade.ddc_product_info c on cast(c.product_id as varchar) = a.product_ids and c.snapshot_version = 'SELLING'
inner join (select t1.par_order_id,max(t1.update_ymd) update_ymd,
sum(case when t4.product2_type = 1 and t5.shop_id is not null then t5.price else t1.order_hosted_price end) cost
from hive.bdc_dwd.dw_mk_order t1
left join hive.bdc_dwd.dw_mk_order_status t2 on t1.order_id = t2.order_id and t2.acct_day = substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
left join mysql7.dataview_trade.mk_order_merchant t3 on t1.order_id = t3.order_id
left join mysql7.dataview_trade.ddc_product_info t4 on t4.product_id = t3.MERCHANT_ID and t4.snapshot_version = 'SELLING'
left join mysql4.dataview_scrm.sc_tprc_product_info t5 on t5.product_id = t4.product_id and t5.shop_id = t1.seller_id
where t1.acct_day = substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
and t2.valid_state in (100,200) ------ 有效訂單
and t1.order_mode = 10 -------- 產品消耗訂單
and t2.complete_state = 1 ----- 訂單已經完成
group by t1.par_order_id
) d on d.par_order_id = b.task_order_id
where c.product_type = 0 and date(from_unixtime(a.last_recipient_time)) > date('2016-01-01') and a.payee_type <> 1 ----------- 已收款
UNION ALL
select ' 企業管家消耗 ' label,date(c.update_ymd) close_ymd,b.product_name,b.product_id,
a.task_id,(case when a.yb_price = 0 and b.product2_type = 1 then b.selling_price_min else a.yb_price end) amt,
(case when a.yb_price =外匯跟單gendan5.com 0 and b.product2_type = 2 then 0 when b.product2_type = 1 and e.shop_id is not null then e.price else c.order_hosted_price end) cost
from mysql8.dataview_tprc.tprc_task a
left join mysql7.dataview_trade.ddc_product_info b on a.product_id = b.product_id and b.snapshot_version = 'SELLING'
inner join hive.bdc_dwd.dw_mk_order c on a.order_id = c.order_id and c.acct_day = substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
left join hive.bdc_dwd.dw_mk_order_status d on d.order_id = c.order_id and d.acct_day = substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
left join mysql4.dataview_scrm.sc_tprc_product_info e on e.product_id = b.product_id and e.shop_id = c.seller_id
where d.valid_state in (100,200) and d.complete_state = 1 and c.order_mode = 10
union ALL
select ' 交易管理系統 ' label,date(t6.close_ymd) close_ymd,t4.product_name,t4.product_id,
t1.order_id,(t1.order_hosted_price-t1.order_refund_price) amt,
(case when t1.order_mode <> 11 then t7.user_amount when t1.order_mode = 11 and t4.product2_type = 1 and t5.shop_id is not null then t5.price else t8.cost end) cost
from hive.bdc_dwd.dw_mk_order t1
left join hive.bdc_dwd.dw_mk_order_business t2 on t1.order_id = t2.order_id and t2.acct_day=substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
left join mysql7.dataview_trade.mk_order_merchant t3 on t1.order_id = t3.order_id
left join mysql7.dataview_trade.ddc_product_info t4 on t4.product_id = t3.MERCHANT_ID and t4.snapshot_version = 'SELLING'
left join mysql4.dataview_scrm.sc_tprc_product_info t5 on t5.product_id = t4.product_id and t5.shop_id = t1.seller_id
left join hive.bdc_dwd.dw_fact_task_ss_daily t6 on t6.task_id = t2.task_id and t6.acct_time=date_format(date_add('day',-1,current_date),'%Y-%m-%d')
left join (select a.task_id,sum(a.user_amount) user_amount
from hive.bdc_dwd.dw_fn_deal_asyn_order a
where a.is_new=1 and a.service='Trade_Payment' and a.state=1 and a.acct_day=substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
group by a.task_id)t7 on t7.task_id = t2.task_id
left join (select t1.par_order_id,sum(t1.order_hosted_price - t1.order_refund_price) cost
from hive.bdc_dwd.dw_mk_order t1
where t1.acct_day = substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2) and t1.order_type = 1 and t1.order_stype = 4 and t1.order_mode = 12
group by t1.par_order_id) t8 on t1.order_id = t8.par_order_id
where t1.acct_day = substring(cast(DATE_ADD('day',-1,CURRENT_DATE) as varchar),9,2)
and t1.order_type = 1 and t1.order_stype in (4,5) and t1.order_mode <> 12 and t4.product_id is not null and t1.order_hosted_price > 0 and t6.is_deal = 1 and t6.close_ymd >= '2018-12-31'
)a
left join mysql7.dataview_trade.ddc_product_info b on a.product_id = b.product_id and b.snapshot_version = 'SELLING'
where b.product2_type = 1 ------- 標品
and close_ymd between DATE_ADD('day',-7,CURRENT_DATE) and DATE_ADD('day',-1,CURRENT_DATE)
GROUP BY b.product_name,
b.selling_price_max,
b.gross_profit_rate_max/100,
b.actrul_supply_num,
case when b.business_type =1 then ' 自營消化 ' when b.business_type =2 then ' 服務商消化 ' end
order by count(a.order_id) desc
limit 10
if __name__ == '__main__':
table_names=[]
sql=get_sqlstr('read_sql.txt')
stmt_tuple=analysis_statements(sql)
for each_stmt in stmt_tuple:
type_name=get_main_functionsql(each_stmt)
blood_table(each_stmt)
Tree_visus(table_names,type_name)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2919624/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 基於 eBPF 的新型追蹤工具:bpftraceeBPF
- Oracle執行語句跟蹤 使用sql trace實現語句追蹤OracleSQL
- 一文搞懂基於zipkin的分散式追蹤系統原理與實現分散式
- 基於SLF4J MDC機制實現日誌的鏈路追蹤
- 微服務追蹤SQL(支援Isto管控下的gorm查詢追蹤)微服務SQLGoORM
- 追蹤解析 Disruptor 原始碼原始碼
- 追蹤解析 ThreadPoolExecutor 原始碼thread原始碼
- 基於 OpenCv 和 Python 的手指識別及追蹤OpenCVPython
- 追蹤解析 Netty IntObjectHashMap 原始碼NettyObjectHashMap原始碼
- Spark SQL 欄位血緣在 vivo 網際網路的實踐SparkSQL
- 用 Rust 實現簡單的光線追蹤Rust
- 基於知識圖譜的APT組織追蹤治理APT
- 微服務追蹤SQL上報至Jaeger(支援Istio管控下的gorm查詢追蹤)微服務SQLGoORM
- 利用Spring Boot實現微服務的鏈路追蹤Spring Boot微服務
- 分散式鏈路追蹤框架的基本實現原理分散式框架
- 如何在 HDRP 中實現光線追蹤?
- Go - 實現專案內鏈路追蹤Go
- 攜程酒店基於血緣後設資料的資料流程最佳化實踐
- 詳解ElasticAPM實現微服務的鏈路追蹤(NET)AST微服務
- 在WebGL中使用GLSL實現光線追蹤Web
- log4j MDC實現日誌追蹤
- Go - 實現專案內鏈路追蹤(二)Go
- IoT 邊緣叢集基於 Kubernetes Events 的告警通知實現
- Linux基礎命令---tracepath追蹤路由Linux路由
- Linux基礎命令---traceroute追蹤路由Linux路由
- 基於OpenTelemetry實現Java微服務呼叫鏈跟蹤Java微服務
- 追蹤解析Spring ioc啟動原始碼(2)Spring原始碼
- 乾貨 | 攜程酒店基於血緣後設資料的資料流程最佳化實踐
- 基於圖資料庫的後設資料血緣關係分析技術研究與實踐資料庫
- 基於 SQL 解析的 JPA 多租戶方案SQL
- Asp.Net Core&Jaeger實現鏈路追蹤ASP.NET
- 自動生成Sql--基於Mybatis的單表SqlSQLMyBatis
- 基於上下文感知計算的APT攻擊組織追蹤方法APT
- Yelp 的 Spark 資料血緣建設實踐!Spark
- 【Golang】基於beego/orm實現相同表結構不同表名的分表方法實現GolangORM
- 使用Spring Cloud Sleuth實現分散式系統的鏈路追蹤SpringCloud分散式
- RTX顯示卡實時光線追蹤技術解析 英偉達RTX顯示卡的光線追蹤技術是什麼?
- 萬字詳解!搜狐智慧媒體基於 Zipkin 和 StarRocks 的微服務鏈路追蹤實踐微服務