我需要一些帮助来编写查询,以显示未向我们报告录音的测量设备列表以及自上次录制以来的天数。
这是表结构:
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
答案 0 :(得分: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旨在确保我们捕获未记录任何内容的设备。