【问题标题】:SQL: Pulling information based on two other columnsSQL:基于另外两列提取信息
【发布时间】:2018-10-28 07:30:49
【问题描述】:

请耐心等待,这有点难以解释: 试图找到一种方法来提取“类型”列中包含“Op”的名称,但“代码”值必须与“类型”是唯一的

如:(使用下面的前3行)

不要拉第一行或第二行,因为“SPX”与“Com”和“Op”都相关,但要拉第 3 行,因为“VPA”在“类型”列中只有“Op”。因此,“code”列下的值不能同时在每个帐户的 type 下的“Op”和“Com”中。

输入”

Name    Type    Code
John    Com     SPX
John    Op      SPX
John    Op      VPA
John    Op      SPX
Matt    Op      SPX
Matt    Op      SPX
Jane    Com     SPX
Jane    Com     SPR
Jack    Op      SPR
Jack    Op      SPX
Jack    Com     SPR

输出:

Name    Type    Code
John    Op      VPA
Matt    Op      SPX
Matt    Op      SPX
Jack    Op      SPX

非常感谢任何帮助!

谢谢!

【问题讨论】:

  • 提示not exists
  • 刚刚根据您的上次更新更新了我的答案..

标签: mysql sql tsql


【解决方案1】:

试试这个NOT EXISTS

select * 
from
mytable t1
where not exists
(
select 1 from mytable t2
where t2.type = 'com'
and t2.name = t1.name   -- if the name exists with a com type then exclude
)

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    相关资源
    最近更新 更多