2017-08-16 24 views
0

Oracle 12.1を実行しています。私はラインアイテムテーブルを持っています。その構造は固定されており、変更することはできません。人が販売地域を見るために、ラインアイテムテーブルの情報のダッシュボードスタイルのページを作成する必要があります。この人物は、大規模な地域を所有するGVP、マネージャー、または個人の担当者である可能性があります。このコピーはDWの一部であるため、ラインアイテムテーブルはかなり標準化されていません。テーブルのこの「コピー」は2週間ごとに更新されるだけで、このようになります。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ごとに1つの行があります。人は、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を追加の基準で使用できますか?特定の業界と特定のADG_STATUSを持つ別個の会社の別名を数えますか?

すべてのアイデアやSQLコードが高く評価されています。

答えて

1

あなたはアンピボット元のデータは、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; 
関連する問題