简体   繁体   English

选择所有在另一列中不匹配的项目

[英]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. 首先,我需要将所有yieldItem 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 itemsection 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-1CT2 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.

相关问题 SQL:我如何在 select 的所有列中至少有 2 个具有特定值的另一列? [使用 mariadb] - SQL: How do I select all of a column that have at least 2 of another column of a specific value? [using mariadb] 如何获得一列中与另一列中的项目匹配的项目计数? - How do I get a count of items in one column that match items in another column? 选择表中未出现在另一个表的外键中的所有项 - Select all items in a table that do not appear in a foreign key of another table SQL-是否可以从一列中选择所有不同的值,而另一列中的所有值都匹配? - SQL - Is it possible to select all Distinct Values from one column where all values from another column match? SQL:获取在另一个表列中具有最大值但还与某些跨表子句匹配的项 - SQL: Getting items which have a max value in another tables column, but also match some cross table clauses 选择与表中所有项目匹配的 ID - Select IDs that match all items on table 选择匹配列表中所有项目的一组行 - Select group of rows that match all items in a list SQL SELECT 仅当所有项目匹配时 - SQL SELECT only when all items match 选择至少与列表中的所有项目匹配的记录 - Select records that match at least all items in a list 添加列通过选择SQL Server中另一个表中的所有项目来选择表 - Add column To select Table by select all items from another table in sql Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM