Home > front end >  Get records only if similar record does not contain a particular value
Get records only if similar record does not contain a particular value

Time:08-10

Here is my example. In below table i need a sql query that can show records that does not contain 101 as value along with 111 , since it has the same Data value. only records i should see is Two and Three which has 111, but does not have 101. basically any record that has 111 but not 101 should be shown, if it has both, then dont show. can anyone help with this query..

enter image description here

CodePudding user response:

try this query

with t(id, val) as (values ('one','101'),('one','111'),('two','111'),('three','111')) 
select * from t where val='111' and id not in (select id from t where val='101')

please provide sample data and expected result hereafter

CodePudding user response:

Considering the structure of your is like:

---------------
| DATA  |TYPE |
---------------
|   One | 101 |
|   One | 111 |
|   Two | 111 |
| Three | 111 |
---------------

(Same as you have given in the image.)

You can write SQL Like:

Select Data from t where TYPE=111
MINUS
Select Data from t where TYPE=101
;
  •  Tags:  
  • sql
  • Related