0

我有一张表格,用于存储特定报告的每日运行情况。

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
4

2 回答 2

0
SELECT fl.NodeId, DATEDIFF(day, fl.FirstDate, fl.LastDate) [Days On] FROM
(SELECT NodeId, MIN(DateAdded) [FirstDate], MAX(DateAdded) [LastDate]
FROM DailyRuns
GROUP BY NodeId
) fl

对于只出现一次的 NodeId,它将显示相同的结果,就好像它的第一次和最后一次出现发生在同一天一样。

SQL 小提琴:http ://sqlfiddle.com/#!3/ea373/7

于 2013-02-28T23:26:21.753 回答
0

这回答了你的问题了吗?

select nodeid, count(distinct dateAdded) as NumDays
from DailyRuns dr
group by nodeid
于 2013-03-01T01:41:01.743 回答