在SQL服务器中编写子查询

时间:2014-07-29 21:07:47

标签: sql-server

如何在SQL SERVER中找到名称如'%Server%'和Status = 1大于5.

Name Status
Server1 0
Server2 1
Server3 1
Server4 1
Server5 1
Server6 1
Server7 0
Database1 1
DataBase2 0

我知道如何编写单独的查询,但不知道如何组合它们。

SELECT Name,Status FROM [dbo].[Jobs_Status](nolock) where Name like 'Server%'

由于 [R

3 个答案:

答案 0 :(得分:0)

您需要使用T-SQL检查计数是否大于5并执行其他操作,如此SQL Fiddle

IF ((select count(*) from jobs where name like 'Server%' and Status = 1) >= 5)
BEGIN
    SELECT 1
END

答案 1 :(得分:0)

也许像

SELECT [Status Count Greater Than 5] = case when Count(*) > 5 then 1 else 0 end
FROM [dbo].[Jobs_Status]
where Name like 'Server%' and Status = 1

答案 2 :(得分:-1)

试试这个:



SELECT Name  
FROM [dbo].[Jobs_Status](nolock) 
where Name like 'Server%' 
 and Status=1
and 
(select count(*) from  [dbo].[Jobs_Status](nolock) where Name like 'Server%' and Status=1) >5