只有在具有一定条件的许多结果时才返回

时间:2014-10-14 13:37:43

标签: sql tsql nested subquery

我已经看了好几个小时了,似乎无法做到这一点。 我有一个有3列的表。

AsOfDate                      database_id    mirroring_state_desc
2014-10-14 09:46:25.083           7             SUSPENDED
2014-10-14 09:47:09.340           7             SUSPENDED
2014-10-14 09:47:10.767           7             SUSPENDED
2014-10-14 09:47:11.987           7             SUSPENDED
2014-10-14 12:34:23.917           7             SUSPENDED
2014-10-14 12:40:11.337           7             SUSPENDED

基本上我将sp放在一起,如果满足某些条件,将发送电子邮件。此实例中的条件是,对于不同的database_id,上述行中有3个或更多行不到一个小时。因此,如果不符合此标准,则不应返回任何内容。

这是我尝试过的。

IF EXISTS (select distinct top (@MirroringStatusViolationCountForAlert) AsOfDate
            from dbo.U_MirroringStatus
            WHERE [AsOfDate] >= dateadd(minute, -60, getdate()))

IF EXISTS (select distinct top 3 AsofDate
from dbo.U_MirroringStatus
WHERE [AsOfDate] >= dateadd(minute, -60, getdate()) IN
    (select AsofDate from dbo.U_MirroringStatus
      GROUP BY AsOfDate HAVING COUNT(*)>=3))

任何帮助都会非常感激,因为我看到的时间越长,我就越感到困惑。

提前致谢。

4 个答案:

答案 0 :(得分:0)

declare @count int = 3
if exists (
            Select  '*' 
            from    dbo.U_MirroringStatus a
            where   @count >= (Select   count(AsOfDate) 
                                from    dbo.U_MirroringStatus b
                                where   [b.AsOfDate] >= dateadd(minute, -60, getdate())
                                and     a.database_id  = b.database_id 
                                )
             )

答案 1 :(得分:0)

IF OBJECT_ID('TestStatus') IS NOT NULL DROP TABLE TestStatus
GO

CREATE TABLE TestStatus (
    AsOfDate DATETIME,
    database_id INT,
    mirroring_state_desc NVARCHAR(100)
)

INSERT INTO TestStatus VALUES 
    ('2014-10-14 04:46:25.083',7,'SUSPENDED'),
    ('2014-10-14 04:47:09.340',7,'SUSPENDED'),
    ('2014-10-14 04:47:10.767',7,'SUSPENDED'),
    ('2014-10-14 09:47:11.987',7,'SUSPENDED'),
    ('2014-10-14 12:34:23.917',7,'SUSPENDED')



IF (SELECT COUNT(database_id) FROM TestStatus WHERE DATEDIFF(hour,AsOfDate,GETDATE()) > 1) > 3
PRINT 'There are more than 3 results which are older than an hour'

答案 2 :(得分:0)

另一种选择..

declare @t table (AsOfDate datetime, database_id int, mirroring_state_desc varchar(20))

insert into @t(AsOfDate, database_id, mirroring_state_desc)
select 
'2014-10-14 08:46:25.083', 7, 'SUSPENDED'
union all select
'2014-10-14 10:47:09.340',           7,             'SUSPENDED'
union all select
'2014-10-14 10:47:10.767',           7,             'SUSPENDED'
union all select
'2014-10-14 10:47:11.987',           7,             'SUSPENDED'
union all select
'2014-10-13 12:34:23.917',           7,             'SUSPENDED'
union all select
'2014-10-13 12:40:11.337',           7,             'SUSPENDED'

IF EXISTS (SELECT 1
    FROM @t
    WHERE AsOfDate >= dateadd(minute, -60, getdate())
    GROUP BY database_id
    HAVING COUNT(*) > 2
    )
    print 'Has 3 or more'

答案 3 :(得分:0)

我相信你在查询中想要这样的东西:

IF EXISTS (
 SELECT top 3 database_id
    FROM dbo.U_MirroringStatus
    WHERE (AsOfDate] >= dateadd(minute, -60, getdate())
    GROUP BY database_id
    HAVING COUNT(*)>=3
)