2016-09-22 68 views
0

一場比賽的所有項目我有這個表選擇沒有另一列

type  item  yield section 
----------------------------------- 
CT1 | A | 25 | M-2 
CT1 | A | 35 | M-1 
CT2 | A | 70 | M-1 
CT2 | A | 30 | M-2 
CT2 | A | 20 | M-3 
CT1 | B | 40 | M-2 
CT1 | B | 15 | M-1 
CT2 | B | 25 | M-1 
CT2 | B | 25 | M-2 

首先我需要Item and Type總結所有yield,並通過每排各產在我的桌子把它。第二我需要知道哪個yield是相同的每costtypcd, item and section(IE type CT1 itemsection M-1具有的0.58(25 + 35 = 60的產率則是35/60 0.58)和type CT2 item。甲section中號-1的產量也是.58,所以我不想選擇那個項目,因爲它是一個匹配項)。

到目前爲止,我有這個代碼,只能拿到收益率(已計算,但無法弄清楚如何只選擇那些items沒有一場比賽在yield,item and section

select type,item,yield, section 
FROM(
Select type, item, 
     Round(yield/Sum(yield) Over (Partition By type,item),2) As yield,section 
From MyTable 
Order By item 
) 

所以我的問題是我怎樣才能得到只有在yield,他們具有相同的item,section and different type一場比賽的所有項目?

在我的表只CT1 A M-1CT2 A M-1是產量的比賽,所以我想選擇所有剩餘項目。

預期輸出:

type  item  section 
-------------------------- 
CT1 | A | M-2 
CT2 | A | M-2 
CT2 | A | M-3 
CT1 | B | M-2 
CT1 | B | M-1 
CT2 | B | M-1 
CT2 | B | M-2  

我並不需要表現出產量。

+0

請出示您的確切預期輸出。 –

+0

@TimBiegeleisen done –

+0

您的示例計算似乎沒有與實際數據加起來。 –

回答

0
with 
    inputs (type, item, yield, section) as (
     select 'CT1', 'A', 25, 'M-2' from dual union all 
     select 'CT1', 'A', 35, 'M-1' from dual union all 
     select 'CT2', 'A', 70, 'M-1' from dual union all 
     select 'CT2', 'A', 30, 'M-2' from dual union all 
     select 'CT2', 'A', 20, 'M-3' from dual union all 
     select 'CT1', 'B', 40, 'M-2' from dual union all 
     select 'CT1', 'B', 15, 'M-1' from dual union all 
     select 'CT2', 'B', 25, 'M-1' from dual union all 
     select 'CT2', 'B', 25, 'M-2' from dual 
    ), 
    prep (type, item, yield, section, pct) as (
     select type, item, yield, section, 
       yield/sum(yield) over (partition by type, item) 
     from inputs 
    ), 
    final (type, item, yield, section, pct, ct) as (
     select type, item, yield, section, pct, 
       count(distinct type) over (partition by item, section, pct) 
     from prep 
    ) 
select type, item, section 
from final 
where ct = 1 
; 

輸出:

TYPE ITEM SECTION 
---- ---- ------- 
CT2 A M-2 
CT1 A M-2 
CT2 A M-3 
CT1 B M-1 
CT2 B M-1 
CT2 B M-2 
CT1 B M-2 

7 rows selected.