Oracle 12c R1 - 我正在嘗試爲銷售組織構建儀表板。具體來說,它用於銷售預測(也稱爲銷售管道)。涉及兩個表,名爲IB_HIERARCHY_S的代表層次結構表和名爲ALL_DEAL的銷售預測表。Oracle - 層級數據的相鄰彙總
IB_HIERARCHY_S表與Oracle附帶的傳統EMP表非常相似...代表具有員工ID,代表具有領土ID,領土ID彙總至其管理者的領土ID。在銷售層次結構中,每人一行,上下導航層次的方式是TERRITORY_ID = PARENT_TERRITORY_ID。它包括整個銷售組織,從最低的代表到經理直到組織副總裁。這個層次結構大約有6層深化。
IB_HIERARCHY_S structure
TERRITORY_ID - primary key
EMAIL_ADDRESS – email address of the rep
PAR_TERR_ID – parent territory ID, aka the territory ID of this person’s manager
…
另一個表是ALL_DEAL。這是一個銷售預測表。每個銷售機會在此表中都有一行。
ALL_DEAL structure
FIELD_REP_E_MAIL – email address of the rep.
TERRITORY_ID – Territory ID of the rep who is forecasting the deal
FISCAL_QUARTER – the quarter the deal is expected to close
TERRITORY_LOB – The line of business who is actively working the deal, Field rep of Telesales Rep
REV_TYPE – One of 6 buckets for the revenue
PIPE – a forecast dollar amount
該問題與顯示/比較樹的多個部分相互作用有關。顯示一個代表如何做的很容易......顯示代表如何對付他的同齡人是一個更具挑戰性的問題。真正的問題是比較彼此不在代表級別的對方,因爲您必須「彙總」和層次結構中其下的每個人的PIPE。假設VP有3個直接報告,'Mgr-A','Mgr-B'和'Mgr-C'。有人使用這個儀表板將輸入'Mgr-A'的電子郵件地址,並希望看到這位經理與'Mgr-B'和'Mgr-C'的比較。我們需要找到Mgr-A的同行,然後爲所有同行彙總層次結構中他們下面的每個人的數據。
我能找到同伴與此SQL
SELECT TERRITORY_ID
FROM IB_HIERARCHY_S
where par_terr_id in
(select par_terr_id FROM IB_HIERARCHY_S
where email_address = 'Mgr-A');
這將返回經理-A,MGR-B和經理-C境內ID。我的問題是如何彙總Mgr-A,Mgr-B和Mgr-C下各個銷售渠道?
這是我到目前爲止的內容,但沒有考慮到Mgr-A,Mgr-B和Mgr-C是組織結構的一部分,並且它們下面的數據需要「捲起來」。
with peer_list as
(SELECT TERRITORY_ID
FROM IB_HIERARCHY_S
where par_terr_id in (select par_terr_id FROM IB_HIERARCHY_S where email_address = 'Mgr-A')
)
select territory_id, fiscal_quarter, territory_lob, rev_type, sum(pipe) as PIPE,
grouping(field_rep_e_mail) as grp_email,
grouping(fiscal_quarter) as grp_1,
grouping(territory_lob) as grp_2,
grouping(rev_type) as grp_3
from smb.all_deal
where fiscal_quarter = 'FY18-Q2'
and TERRITORY_ID in (select territory_ID from peer_list)
group by rollup(territory_id, fiscal_quarter, territory_lob, rev_type)
order by territory_id, fiscal_quarter, territory_lob, rev_type;
任何幫助或想法讚賞。
ANSWER
什麼最後的工作是識別(通過CONNECT BY查詢),所有的根和葉全部電子郵件。然後,我總結了葉郵件中的ALL_DEAL,但通過根郵件分組。
with peer_list as
(SELECT distinct email_address,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH(territory_id, '/'), '[^/]+', 1, 1) Root_Terr_Id,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH(email_address, '/'), '[^/]+', 1, 1) Root_Email
FROM IB_HIERARCHY_S
START WITH territory_id in
(SELECT TERRITORY_ID
FROM IB_HIERARCHY_S
where par_terr_id in
(select par_terr_id FROM IB_HIERARCHY_S
where email_address = 'Mrg-A'))
CONNECT BY PRIOR territory_id = par_terr_id
order by email_address
)
select <data> from ALL_DEAL, PEER_LIST
...
請創建[最小,完整,可驗證的示例](https://stackoverflow.com/help/mcve)。 – krokodilko