選擇position_no,
position_function,
work_function,
job_profile_id,
pos_cat position_category,
pos_cat_desc,
job_posting_title pos_title,
employee_status emp_status,
emp_status_desc,
clevel,
substr (sys_connect_by_path (fund_code, ' < '), 3) doa_path
substr (sys_connect_by_path (position_no, ' < '), 3) rep_path
connect_by_root運算(manager_position_id) root_parent,
之前fund_code PRIOR_NON_NAFUND_CODE,
之前manager_position_id PRIOR_MGR_POS_ID,
99級,
manager_position_id,
fund_code,
executive_supervisor
(選擇p。position_no, p。manager_position_id, p。fund_code, p。position_function p.work_function,
p.job_profile_id,p。pos_cat, p。pos_cat_desc, p。job_posting_title, p。employee_status p.emp_status_desc,
p.clevel, p。從表p executive_supervisor
在當前日期之間p。position_effective_date和p.pos_end_date
和p。IUD_FLAG在(“我”、“U”)) p
在之前fund_code是零
CONNECT BY NOCYCLE position_no = manager_position_id之前
嗨@lakshmi年代,所提供的代碼轉換成磚SQL,您可以使用以下等價查詢:
與遞歸cte(選擇position_no、position_function work_function, job_profile_id, pos_cat position_category, pos_cat_desc, job_posting_title pos_title, employee_status emp_status, emp_status_desc, clevel,演員(fund_code字符串)doa_path,演員(position_no字符串)rep_path, manager_position_id root_parent, PRIOR_NON_NAFUND_CODE零,零PRIOR_MGR_POS_ID, 99級,manager_position_id, fund_code, executive_supervisor position_effective_date之間從表當前日期和pos_end_date IUD_FLAG(“我”、“U”)聯盟所有選擇p。position_no, p。position_function p.work_function,p.job_profile_id,p。pos_cat position_category, p。pos_cat_desc, p。job_posting_title pos_title, p。employee_status emp_status, p。emp_status_desc, p。clevel CONCAT (cte。doa_path,'<-', CAST(p.fund_code AS STRING)) AS doa_path, CONCAT(cte.rep_path, '<-', CAST(p.position_no AS STRING)) AS rep_path, cte.root_parent, cte.fund_code AS PRIOR_NON_NAFUND_CODE, cte.manager_position_id AS PRIOR_MGR_POS_ID, cte.lvl + 1 AS lvl, p.manager_position_id, p.fund_code, p.executive_supervisor FROM TABLE p JOIN cte ON PRIOR position_no = manager_position_id ) SELECT position_no, position_function, work_function, job_profile_id, position_category, pos_cat_desc, pos_title, emp_status, emp_status_desc, clevel, doa_path, rep_path, root_parent, PRIOR_NON_NAFUND_CODE, PRIOR_MGR_POS_ID, lvl, manager_position_id, fund_code, executive_supervisor FROM cte WHERE PRIOR fund_code IS NULL CONNECT BY NOCYCLE PRIOR position_no = manager_position_id
“表”替換為實際的表名或表別名磚環境中。同時,注意磚SQL支持遞歸查詢使用遞歸的條款類似於其他數據庫係統中使用遞歸的語法。
請調整根據您的特定的表和列的名稱,代碼和磚SQL的環境中測試它,確保它產生期望的結果。
請試試這個,看看這個或不工作
——創建一個臨時表來存儲遞歸的結果
創建或替換臨時視圖recursive_results
選擇p.position_no,
p.position_function,
p.work_function,
p.job_profile_id,
p。pos_cat position_category,
p.pos_cat_desc,
p。job_posting_title pos_title,
p。employee_status emp_status,
p.emp_status_desc,
p.clevel,
p.fund_code,
p.manager_position_id,
p.executive_supervisor,
(p。position_no rep_path字符串),
(p。fund_code doa_path字符串),
99級,
(p。manager_position_id root_parent字符串),
鑄像prior_non_nafund_code(空字符串),
鑄像prior_mgr_pos_id(空字符串)
從表
在當前日期之間p。position_effective_date和p.pos_end_date
和p。IUD_FLAG在(“我”、“U”);
——迭代自連接,直到沒有更多的結果
創建或替換臨時視圖recursive_temp
SELECT *
從recursive_results;
創建或替換臨時視圖recursive_temp_new
選擇p.position_no,
p.position_function,
p.work_function,
p.job_profile_id,
p。pos_cat position_category,
p.pos_cat_desc,
p。job_posting_title pos_title,
p。employee_status emp_status,
p.emp_status_desc,
p.clevel,
p.fund_code,
p.manager_position_id,
p.executive_supervisor,
CONCAT (r。rep_path,'<-', CAST(p.position_no AS STRING)) AS rep_path,
CONCAT (r。doa_path,'<-', CAST(p.fund_code AS STRING)) AS doa_path,
99級,
r.root_parent,
r。fund_code prior_non_nafund_code,
r。manager_position_id作為prior_mgr_pos_id
從表
加入recursive_temp r r。position_no = p.manager_position_id;
——重複自連接,直到沒有更多的結果
而從recursive_temp_new (SELECT COUNT (*)) > 0
插入表recursive_temp recursive_temp_new SELECT *;
截斷表recursive_temp_new;
插入表recursive_temp_new
選擇p.position_no,
p.position_function,
p.work_function,
p.job_profile_id,
p。pos_cat position_category,
p.pos_cat_desc,
p。job_posting_title pos_title,
p。employee_status emp_status,
p.emp_status_desc,
p.clevel,
p.fund_code,
p.manager_position_id,
p.executive_supervisor,
CONCAT (r。rep_path,'<-', CAST(p.position_no AS STRING)),
CONCAT (r。doa_path,'<-', CAST(p.fund_code AS STRING)),
99級,
r.root_parent,
r。fund_code prior_non_nafund_code,
r。manager_position_id作為prior_mgr_pos_id
從表
加入recursive_temp r r。position_no = p.manager_position_id;
結束時;
——最後的查詢來檢索結果
選擇position_no,
position_function,
work_function,
job_profile_id,
position_category pos_cat,
pos_cat_desc,
pos_title job_posting_title,
emp_status,
emp_status_desc,
clevel,
doa_path,
rep_path,
root_parent,
prior_non_nafund_code,
prior_mgr_pos_id,
級,
manager_position_id,
fund_code,
executive_supervisor
從recursive_temp
prior_non_nafund_code為空;
另一種方法使用臨時視圖來模擬遞歸的行為。它迭代執行可利用並將結果插入臨時表,直到沒有更多的結果。最後,從臨時表獲取所需的結果。