简体   繁体   中英

How to filter a table based on a specific column value of another column?

Hello I have a table here

---------------------
ID | PARTY | Name.Id
---------------------
1  | IND   | 12
2  | IND   | 13
3  | CUST  | 14
4  | CUST  | 15
5  | CUST  | 16
6  | IND   | 17
---------------------

I want to return the whole table but filter 'CUST' which has value 15 and 16 in the column 'Name.Id'

The result should look something like this

---------------------
ID | PARTY | Name.Id
---------------------
1  | IND   | 12
2  | IND   | 13
3  | CUST  | 14
4  | IND   | 17
---------------------

I tried using where statement on the 'Name.Id' but it returns only those rows which has a value 15 and 16.

whole table but filter 'CUST' which has value 15 and 16 in the column 'Name.Id'

It sounds like you want a where clause like:

WHERE NOT (party = 'CUST' and name.id IN(15,16))

But CUST seems redundant from your sample data, ie you could get away with simplifying it to just WHERE name.id NOT IN (15,16)

Can you give this a whirl?

select *
from t
where (PARTY, Name.Id) not in (
     select 'CUST', 15
     union
     select 'CUST', 16
)
select *
from t
    where Party not in
        (select Party from t
            where Party="CUST" 
                and (Name.Id=15 or Name=16));

I think I got the solution here

select *
from t
where Party <> 'CUST'
or (Party = 'CUST'
and Name.Id = 14))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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