[英]Select all items that do not have a match in another column
I have this table 我有这张桌子
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
First I need to sum all yield
by Item and Type
and divide it by each yield per row on my table. 首先,我需要将所有
yield
按Item and Type
求和Item and Type
然后除以表上每行的收益。 Second I need to know which yield
is the same per costtypcd, item and section
(IE type
CT1 item
A section
M-1 has a yield of .58 (25+35=60 then 35/60 is .58) and type
CT2 item
. A section
M-1 also has a yield of .58 so I don't want to select that item because it's a match). 第二我需要知道哪个
yield
是每同一costtypcd, item and section
(IE type
CT1 item
阿section
M-1具有的0.58(25 + 35 = 60的产率则是35/60 0.58)和type
CT2 item
。M-1 section
的收益也为.58,因此我不想选择该项目,因为它是匹配项)。
So far I have this code that can only get the yield (already computed but can't figure out how to select only those items
that do not have a match in yield,item and section
到目前为止,我有这段代码只能获取收益(已计算出但无法弄清楚如何仅选择
yield,item and section
不匹配的那些items
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
)
So my question is how can I get all items that only has a match in yield
where they have the same item,section and different type
? 因此,我的问题是,如何在
yield
相同的所有项目中使用相同的item,section and different type
?
In my table only CT1 A M-1
AND CT2 A M-1
is a match in yield so I'd like to select all the remaining items. 在我的表格中,只有
CT1 A M-1
和CT2 A M-1
在产量上是匹配的,所以我想选择所有剩余的项目。
Expected Output: 预期产量:
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
I don't need to show the yield. 我不需要证明产量。
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
;
Output: 输出:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.