Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在sql server中基于列的值获取行_Sql_Sql Server - Fatal编程技术网

如何在sql server中基于列的值获取行

如何在sql server中基于列的值获取行,sql,sql-server,Sql,Sql Server,我想获取其部门评级至少有一个值如“p%”和一个值如“T%”的所有行。存在一种相当直接的方法: @维沙。这是一个关联子句,用于对具有相同部门的行进行比较。。为什么选择1选择1?@vishal。exists仅检查行的存在性。通常,一行需要有一些值。我发现1是最容易键入的。请从表_name nolock中选择*,其中的评级为“P%”或“T%”@Gordon Linoff。我想获取部门评级至少有一个值为“T%”且没有“P%”的所有行。我尝试从存在的t中选择t.*从t2中选择1,其中t2.dept=t.d

我想获取其部门评级至少有一个值如“p%”和一个值如“T%”的所有行。

存在一种相当直接的方法:


@维沙。这是一个关联子句,用于对具有相同部门的行进行比较。。为什么选择1选择1?@vishal。exists仅检查行的存在性。通常,一行需要有一些值。我发现1是最容易键入的。请从表_name nolock中选择*,其中的评级为“P%”或“T%”@Gordon Linoff。我想获取部门评级至少有一个值为“T%”且没有“P%”的所有行。我尝试从存在的t中选择t.*从t2中选择1,其中t2.dept=t.dept,t2.rating不象'P%'并且存在从t2中选择1,其中t2.dept=t.dept,t2.rating象't%';但无法得到预期的结果。
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
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%') ;