如何基于列的值在SQL Server中获取行

时间:2018-10-04 21:06:46

标签: sql sql-server

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

我想获取其部门的评分至少具有一个像'P%'和一个像'T%'的行。

1 个答案:

答案 0 :(得分:1)

一个相当直接的方法使用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%') ;