我有一张表格,用于存储特定报告的每日运行情况。
DateAdded NodeId Field1Value Field1Threshold etc
我现在有一个新要求,即列出报告中出现 NodeId 的天数。因此,如果 DateAdded 被列为
2013-03-01 <-- Run date
2013-02-28
2013-02-27
2013-02-26 <-- End of date sequence. Return 4 days
2013-02-13
2013-01-07
这也是关于识别日期序列中的中断
编辑我的时间上午 10:54 这是我的 sp 中带有第一个建议的 where 子句
t.etc,
t.blah,
CASE WHEN ras1.NodeId IS NULL THEN 'N' ELSE 'Y' END as AlertStat,
fl.NodeId as fl_NodeId,
DATEDIFF(day, fl.LastDate, fl.FirstDate) as [DaysOn]
FROM vwTrips t left join
(
select distinct nodeId
from CustLogs.dbo.ReportAlertStats ras
where ras.DateAdded between @RasStart and @RasEnd
) ras1 on t.NodeId = ras1.NodeId left join
( SELECT NodeId,
MIN(DateAdded) [FirstDate],
MAX(DateAdded) [LastDate]
FROM CustLogs.dbo.ReportAlertStats
GROUP BY NodeId
--ORDER BY NodeId, DateAdded
) fl on t.NodeId = fl.NodeId
WHERE t.TripStart between @ReportStart and @ReportEnd