Oracle SQL - 计算不同的列组合

时间:2017-08-16 13:28:12

标签: sql oracle

运行Oracle 12.1。我有一个行项目表。它的结构是固定的,我无法改变它。我需要构建一个仪表板样式的行项目表信息页面,供人查看其销售区域。此人可能是GVP,拥有大片领土,或经理或个人代表。行项目表非常不规范化,因为此副本是DW的一部分。该表的“副本”仅每2周更新一次,看起来像这样。

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字段中列出的每个独特人物。以下是最终结果表的外观。除了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和其他标准吗? Aka是否具有特定行业和特定ADG_STATUS的独特公司?

最受赞赏的任何想法或SQL代码。

1 个答案:

答案 0 :(得分:1)

您可以取消原始数据,以便将原始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;