SQL:根据另外两列来提取信息

时间:2018-05-17 22:42:26

标签: mysql sql tsql

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

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

不要拉第一行或第二行,因为“SPX”与“Com”和“Op”都相关,但是拉行#3,因为“VPA”在“类型”列中只有“Op”。因此,“代码”列下的值不能同时属于每个帐户类型的“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

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个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
)
相关问题