简体   繁体   中英

how to fetch rows in sql server based on values of column

id      dept         Person       Rating
-------------------------------------------
1       ece            p1           R1  
2       ece            p2           t1
3       eee            P3           R2             
4       eee            p4           M
5       Civil          P5           R2
6       Civil          P6           t2
7       Civil          P7           t2
8       Mech           p8           R2
9       Mech           P9           NULL
10      IT             P10          R2J
11      IT             P11          T2
12      IT             P12          T2

I would like to fetch all the rows whose department's rating has at least one value like 'P%' and one like 'T%'.

A rather direct method uses exists :

select t.*
from t
where exists (select 1 from t t2 where t2.dept = t.dept and t2.rating like 'P%') and
      exists (select 1 from t t2 where t2.dept = t.dept and t2.rating like 'T%') ;

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