0

我需要一些帮助来编写查询以显示尚未向我们报告记录的测量设备列表以及自上次记录以来的天数。

这是表结构:

select d.deviceno, r.lastrecordingdate, --Here's where i need a table to act as a COUNT for --number of days the device has not logged.
from modules m
join devices d on m.deviceid = d.deviceid
join recordings r on m.recordingid = r.recordingid
--i really don't know what to write beyond this. I am drawin
4

1 回答 1

0
SELECT D.deviceno, 
       DateDiff(day, CURRENT_TIMESTAMP, max_date) AS days_since
  FROM (
          SELECT D.deviceno,
                 COALESCE(MAX(R.last_recordingdate), '2000-01-01') AS max_date
            FROM Modules M
                 INNER JOIN Devices D
                    ON M.deviceid = D.deviceid
                 LEFT JOIN Recordings R 
                    ON M.recordingid = R.recordingid
                 GROUP BY D.deviceno
        ) FOO
  WHERE max_date < DateAdd(day, -7, CURRENT_TIMESTAMP)

由于您没有指定数据库,因此我使用的是 SQL Server 特定语法(日期数学函数)。COALESCE 是为了确保我们捕获根本没有记录任何内容的设备。

于 2013-07-12T14:48:22.610 回答