SQL查询的日期时间在5分钟之内

时间:2011-05-03 16:47:30

标签: sql sql-server

我正在尝试设置一个报告,在五分钟内查询数据。这样,我就能认出我的一台机器坏了。这就是我到目前为止所做的:

   SELECT A_M_DEVICE.M_Device_ID,
   A_M_DEVICE.Machine_Name,
   A_M_DEVICE.IP_Address,
   A_M_DEVICE.Device_Type,
   A_M_DEVICE.M_Device_Status_ID,
   A_M_DEVICE.Status_Date,
   S_M_DEVICE_STATUS.M_Device_Status_Desc,
   A_M_DEVICE.Domain_Name FROM    MConsole.dbo.A_M_DEVICE A_M_DEVICE
   INNER JOIN
      MConsole.dbo.S_M_DEVICE_STATUS S_M_DEVICE_STATUS
   ON (A_M_DEVICE.M_Device_Status_ID =
          S_M_DEVICE_STATUS.M_Device_Status_ID) WHERE (A_M_DEVICE.Machine_Name IN
       ('DeVA',
        'DevB',
        ))
   AND (A_M_DEVICE.Status_Date = DateDiff (Second) <= 300)

因为我是新手,所以不允许拍照。否则我会张贴一个。

好吧 - 看起来我对截图有足够的声誉! enter image description here 任何帮助,一如既往,将受到高度赞赏。

提前谢谢。

2 个答案:

答案 0 :(得分:2)

这是一个近似值 - 检查“&lt; 5”与“&gt; 5”等。

SELECT M_Device_ID,
    Machine_Name,
    IP_Address,
    Device_Type,
    M_Device_Status_ID,
    M_Device_Status_Desc,
    Domain_Name
FROM
(   SELECT M_Device_ID,
        Machine_Name,
        IP_Address,
        Device_Type,
        M_Device_Status_ID,
        M_Device_Status_Desc,
        Domain_Name,
        MAX(Status_Date)
    FROM A_M_DEVICE
    GROUP BY
        M_Device_ID,
        Machine_Name,
        IP_Address,
        Device_Type,
        M_Device_Status_ID,
        M_Device_Status_Desc,
        Domain_Name
) AS a
JOIN A_M_DEVICE AS b
ON
     a.Machine_Name = b.Machine_Name
     AND a.IP_Address = b.IP_Address
     AND a.Device_Type = b.Device_Type
     AND a.M_Device_Status_ID = b.M_Device_Status_ID
     AND a.M_Device_Status_Desc = b.M_Device_Status_Desc
     AND a.Domain_Name = b.Domain_Name
WHERE
     DATEDIFF(minute, a.Status_Date, b.Status_Date) < 5

答案 1 :(得分:1)

选择状态日期与MAX状态日期不匹配的行:

SELECT A_M_DEVICE.M_Device_ID,
   A_M_DEVICE.Machine_Name,
   A_M_DEVICE.IP_Address,
   A_M_DEVICE.Device_Type,
   A_M_DEVICE.M_Device_Status_ID,
   A_M_DEVICE.Status_Date,
   S_M_DEVICE_STATUS.M_Device_Status_Desc,
   A_M_DEVICE.Domain_Name FROM    MConsole.dbo.A_M_DEVICE A_M_DEVICE
   INNER JOIN
      MConsole.dbo.S_M_DEVICE_STATUS S_M_DEVICE_STATUS
   ON (A_M_DEVICE.M_Device_Status_ID =
          S_M_DEVICE_STATUS.M_Device_Status_ID) WHERE (A_M_DEVICE.Machine_Name IN
       ('DeVA',
        'DevB',
        ))
   AND (A_M_DEVICE.Status_Date NOT IN (SELECT MAX(A_M_DEVICE.Status_Date) FROM A_M_DEVICE)

对于表示可运行的计算机的所有行,日期将相同 - 因此,消除与MAX日期匹配的行会留下代表尚未进行状态更新的计算机的记录。