2017-08-16 43 views
0

運行Oracle 12.1。我有一個行項目表。它的結構是固定的,我不能改變它。我需要爲某人查看其銷售地區,創建行項目表信息的儀表板樣式頁面。這個人可能是一個GVP,擁有一個大的領土,或一個經理,或一個個人代表。行項目表格非常規範化,因爲此副本是DW的一部分。該表的「副本」每兩週更新一次,看起來像這樣。Oracle SQL - 計數不同的列組合

Line_Item_ID // PK 
Account_ID // 
Company_Name // The legal name of the Headquarters 
LOB_Name // Line of business, aka Division within the Company_Name 
Account_Type // One of 2 values, ‘NAMED’ or 「GENERAL’ 
ADG_STATUS // 3 possible values, ‘A’, ‘D’ or ‘G’ 
Industry // One of 15 values, for this example assume it is ONLY ‘MFG’, ‘GOV’, ‘HEALTHCARE’ 
// Now have the sales hierarchy of the rep who sold this 
GVP // Group Vice President 
SVP // Sales Vice President 
RVP // Regional Vice President 
RM // Regional Manager 
REP // Sales Rep 
// Now have information about the product sold 
ProductName 
ProductPrice 
VariousOtherFields…. 

我需要製作一個彙總表,用於快速訪問儀表板。它將包含各種組合的計數,並且每個PERSON將有一行,而不是賬戶。一個人是任何GVP,SVP,RVP,RM或REP字段中列出的每個UNIQUE人。以下是最終結果表的樣子。除PERSON外,每列都基於DISTINCT計數,並且是一個整數值。

PERSON 
TOTAL_COMPANIES // For this person, count of DISTINCT COMPANY_NAME 
TOTAL_LOBS // For this person, count of DISTINCT LOBS 
TOTAL_COMPANIES_NAMED // count of DISTINCT COMPANY_NAME with ACCOUNT_TYPE=’NAMED’ 
TOTAL_COMPANIES_GENERAL // count of DISTINCT COMPANY_NAME with ACCOUNT_TYPE=’GENERAL’ 
TOTAL_LOBS_NAMED // count of DISTINCT LOB_NAME with ACCOUNT_TYPE=’NAMED’ 
TOTAL_LOBS_GENERAL // count of DISTINCT LOB_NAME with ACCOUNT_TYPE=’GENERAL’ 
TOTAL_COMPANIES_STATUS_A // count of DISTINCT COMPANY_NAME with ADG_STATUS=’A’ 
TOTAL_COMPANIES_STATUS_D // count of DISTINCT COMPANY_NAME with ADG_STATUS=’D’ 
TOTAL_COMPANIES_STATUS_G // count of DISTINCT COMPANY_NAME with ADG_STATUS=’G’ 
TOTAL_LOB_STATUS_A // count of DISTINCT LOB_NAME with ADG_STATUS=’A’ 
TOTAL_LOB_STATUS_D // count of DISTINCT LOB_NAME with ADG_STATUS=’D’ 
TOTAL_LOB_STATUS_G // count of DISTINCT LOB_NAME with ADG_STATUS=’G’ 
//Now Various Industry Permutations. I have 15 different industries, but only showing 2. This will only be at the COMPANY_NAME level, not the LOB_NAME level 
MFG_COMPANIES_STATUS_A // count of DISTINCT COMPANY_NAME with ADG_STATUS=’A’ and Industry = ‘MFG’ 
MFG_COMPANIES_STATUS_D // count of DISTINCT COMPANY_NAME with ADG_STATUS=’D’ and Industry = ‘MFG’ 
MFG_COMPANIES_STATUS_G // count of DISTINCT COMPANY_NAME with ADG_STATUS=’G’ and Industry = ‘MFG’ 

GOV_COMPANIES_STATUS_A // count of DISTINCT COMPANY_NAME with ADG_STATUS=’A’ and Industry = ‘GOV’ 
GOV_COMPANIES_STATUS_D // count of DISTINCT COMPANY_NAME with ADG_STATUS=’D’ and Industry = ‘GOV’ 
GOV_COMPANIES_STATUS_G // count of DISTINCT COMPANY_NAME with ADG_STATUS=’G’ and Industry = ‘GOV’ 

有約。行項目表中有400人,35000個唯一帳戶和200,000個條目。

那麼我的策略是什麼?我曾考慮製作另一張獨特的PERSON值表,並將其用作駕駛臺。我們稱這個表爲PERSON_LIST。

Pseudo-code… 

For each entry in PERSON_LIST 
    For all LINE_ITEMS where person_list in ANY(GVP, SVP, RVP, RM, REP) do 
     Calculations… 

這將是一個令人難以置信的長期運行的進程......

我怎麼能做到這一點更有效(一套基於而不是通過逐行)?我相信我將不得不使用PIVOT操作員作爲行業名單,但是我可以使用PIVOT和其他標準嗎?又名不同公司與特定行業和特定ADG_STATUS?

任何想法或SQL代碼最讚賞。

回答

1

你可以unpivot的原始數據從原始GVP數據等列成一個「人」欄:

select * from line_items 
unpivot (person for role in (gvp as 'GVP', svp as 'SVP', rvp as 'RVP', 
    rm as 'RM', rep as 'REP')) 

然後使用它作爲CTE或內嵌視圖,漂亮你展示的很多;使用案例表達式進行條件彙總,例如:

select person, 
    count(distinct company_name) as total_companies, 
    count(distinct lob_name) as total_lobs, 
    count(distinct case when account_type='NAMED' then company_name end) 
    as total_companies_named, 
    count(distinct case when account_type='GENERAL' then company_name end) 
    as total_companies_general, 
    count(distinct case when account_type='NAMED' then lob_name end) 
    as total_lobs_named, 
    count(distinct case when account_type='GENERAL' then lob_name end) 
    as total_lobs_general, 
    count(distinct case when adg_status='A' then company_name end) 
    as total_companies_status_a, 
    count(distinct case when adg_status='D' then company_name end) 
    as total_companies_status_d, 
    count(distinct case when adg_status='G' then company_name end) 
    as total_companies_status_g, 
    count(distinct case when adg_status='A' then lob_name end) 
    as total_lob_status_a, 
    count(distinct case when adg_status='D' then lob_name end) 
    as total_lob_status_d, 
    count(distinct case when adg_status='G' then lob_name end) 
    as total_lob_status_g, 
    count(distinct case when adg_status='A' and industry = 'MFG' then company_name end) 
    as mfg_companies_status_a, 
    count(distinct case when adg_status='D' and industry = 'MFG' then company_name end) 
    as mfg_companies_status_d, 
    count(distinct case when adg_status='G' and industry = 'MFG' then company_name end) 
    as mfg_companies_status_g, 
    count(distinct case when adg_status='A' and industry = 'GOV' then company_name end) 
    as gov_companies_status_a, 
    count(distinct case when adg_status='D' and industry = 'GOV' then company_name end) 
    as gov_companies_status_d, 
count(distinct case when adg_status='G' and industry = 'GOV' then company_name end) 
    as gov_companies_status_g 
from (
    select * from line_items 
    unpivot (person for role in (gvp as 'GVP', svp as 'SVP', rvp as 'RVP', 
    rm as 'RM', rep as 'REP')) 
) 
group by person;