简体   繁体   English

SQL 查询返回与具有特定值的另一列关联的列

[英]SQL query to return column that is associated to another column with a certain value

COL1 COL1 COL2 COL2
10068 10068 CP CP
10068 10068 DF东风
10068 10068 DP DP
10069 10069 CP CP
10061 10061 CP CP

I have this table and I am trying to write a SQL query in Oracle, such that it fetches col1s that have corresponding col2 as 'CP' only and nothing else.我有这个表,我正在尝试在 Oracle 中编写一个 SQL 查询,以便它只获取对应 col2 为“CP”的 col1,仅此而已。 In above example, data returned should be 10069 and 10061, it should not return 10068 as it has DF and DP as well associated.在上面的例子中,返回的数据应该是 10069 和 10061,它不应该返回 10068,因为它有 DF 和 DP 以及关联。

Below is my query but it doesn't work, it is fetching col1 with 10068 as well下面是我的查询,但它不起作用,它也在用 10068 获取 col1

SELECT DISTINCT COL1, COL2 
FROM SAMPLETABLE 
WHERE COL2 NOT IN ('DF', 'DP')

Can someone help please?有人可以帮忙吗?

You can use group by and having :您可以使用group byhaving

select col1
from sampletable t
group by col1
having min(col2) = max(col2) and min(col2) = 'CP'

You can use not exists你可以使用not exists

 create table SAMPLETABLE ( COL1 int, COL2 varchar(10));


 insert into SAMPLETABLE values(10068,  'CP');
 insert into SAMPLETABLE values(10068,  'DF');
 insert into SAMPLETABLE values(10068,  'DP');
 insert into SAMPLETABLE values(10069,  'CP');
 insert into SAMPLETABLE values(10061,  'CP');

Query:询问:

 select col1,col2 from sampletable a
 where not exists
                 (select 1 from sampletable b where a.col1=b.col1 and b.col2<'CP')

Output: Output:

COL1 COL1 COL2 COL2
10061 10061 CP CP
10069 10069 CP CP

Or you can also use group by with having clause:或者您也可以使用 group by with 子句:

Query:询问:

select col1  from SAMPLETABLE 
 group by col1
 having count(distinct col2)=1
 and min(col2) ='CP'

Output: Output:

COL1 COL1
10061 10061
10069 10069

db<>fiddle here db<> 在这里摆弄

暂无
暂无

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

相关问题 在 SQL 中,我可以在另一列中获取与它们没有关联的特定值的列中的值吗? - In SQL, can I get the values in one column that don't have a certain value associated to them in another column? SQL 查询关联列 - SQL query with associated column SQL Server查询以选择与其他列中的唯一值关联的列 - SQL server query to select column associated to a unique value in other column 如何在SQL查询中选择最高列值,如果另一列等于某个字符串 - How do I select the highest column value in an SQL query IF another column is equal to a certain string 不确定如何编写查询以返回与group by中的最大值关联的列 - Not sure how to write a query to return column associated with max value in group by SQL查询以确定某个列值的优先级 - SQL query to prioritize a certain column value Oracle-SQL查询以从另一个表中的列返回值,其中值等于另一个列 - Oracle - SQL Query to return a value from a column in another table where value equals a different column 从表中获取最小总和值和关联列的 SQL 查询 - SQL query to get minimal sum value and associated column from table SQL查询返回的列的值大于某个最小值(取决于类别)的行? - SQL query to return rows with the value of a column above a certain minimum value depending on category? SQL查询返回一个列值,其中每个序列号的另一列最少 - SQL query to return a column value where another column is minimum per serial number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM