简体   繁体   English

根据列和SQL中的两个条件选择不同的行

[英]Select distinct rows based on column and two conditions in SQL

I would like to retrieve rows from this data set where a T1/T3 value exists, but no T2/T3 value exists for a corresponding ID. 我想从存在T1 / T3值但不存在对应ID的T2 / T3值的该数据集中检索行。

ID  sample1 sample2 value
A_000002    T2  T3  -0.934119
A_000002    T1  T3  -0.866637
A_000029    T2  T3  -1.07677
A_000037    T2  T3  -0.76506
A_000057    T1  T3  -5.34988

I'd like to say something like: 我想说些类似的话:

SELECT * FROM table
WHERE DISTINCT ID
AND sample_1 == "T1"
AND sample_2 == "T3"

...and return only the following because it has no corresponding T2/T3 row for that ID: ...并仅返回以下内容,因为该ID没有对应的T2 / T3行:

A_000057    T1  T3  -5.34988

If I use sample_1 and sample_2 conditions, I get distinct values anyway because it filters out the the "T2" values before checking if the ID is distinct. 如果我使用sample_1和sample_2条件,无论如何我都会得到不同的值,因为它会在检查ID是否不同之前过滤掉“ T2”值。

The closest I've come is to make 3 tables with the possible T1/T2/T3 combinations and screen for NOT EXISTS T1T2.ID = T2T3.ID 我最接近的是用可能的T1 / T2 / T3组合制作3张桌子,并在屏幕上显示NOT EXISTS T1T2.ID = T2T3.ID

select * from T1T2
where not exists (select * from T2T3 where T2T3.id = T1T2.id)
and not exists (select * from T1T3 where T1T3._id = T1T2.id)
order by id

I'm not sure I trust the code yet though. 我不确定我是否相信代码。

You can use not exists : 您可以使用not exists

select t.*
from table t
where (sample1 = 'T1' and sample2 = 'T3') and
       not exists (select 1 
                   from table t1 
                   where t1.id = t.id and 
                         t1.sample1 = 'T2' and t1.sample2 = 'T3'
                  );

You can use this technique based on an outer join: 您可以基于外部联接使用此技术:

select t1.*
from table t1
left join table t2
on t2.id = t1.id
  and t2.sample1 = 'T2' and t2.sample2 = 'T3'
where t1.sample1 = 'T1' and t1.sample2 = 'T3'
and t2.id is null

It works because outer joins return nulls if there's no join, and you return only those via the t2.id is null condition in the where clause. 之所以有效,是因为如果没有联接,则外部联接返回null,而您仅通过t2.id is null返回where子句中的条件。

The big advantage of this technique is the efficient use of the index on the id column; 该技术的最大优点是有效利用了id列上的索引; this technique will generally out-perform other apporaches. 该技术通常会胜过其他方法。 Plus IMHO it's a neater looking query. 再加上恕我直言,这是一个更整洁的查询。

Note that the condition for the sample columns must be in the join condition for t2 , otherwise you'd effectively get an inner join, defeating the required outer join. 请注意, sample列的条件必须处于t2联接条件中,否则您将有效地获得内部联接,从而击败了所需的外部联接。

I would use not exists : 我会使用not exists

SELECT t.*
FROM table t
WHERE t.sample_1 = 'T1' AND t.sample_2 = 'T3' AND
      NOT EXISTS (SELECT 1 FROM table t2 WHERE t2.id = t.id);

Here will surly work: 这将是正常的工作:

Select distinct ID, * from table Where sample1 = 't1' and sample2 = 't3' 从表中选择不同的ID *,其中sample1 ='t1'和sample2 ='t3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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