简体   繁体   中英

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. 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).

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

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 ?

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.

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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM