2017-08-16 94 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;