每个zone_id的sql最大日期

时间:2019-01-29 16:16:50

标签: sql

使用以下查询:

SELECT     evhist_XPK.event_date, evhist_XPK.zone_id, system.cs_no, evhist_XPK.event_id, event.descr
FROM         evhist_XPK INNER JOIN
             system ON evhist_XPK.system_no = system.system_no INNER JOIN
             site ON system.site_no = site.site_no INNER JOIN
             event ON event.event_id=evhist_XPK.event_id
WHERE        (evhist_XPK.event_id = '7381')  AND (system.systype_id like 'V%') AND (evhist_XPK.event_date >= DATEADD(day, DATEDIFF(day, 7, GETDATE()), 0)) AND (site.sitestat_id IN('A','PF'))
ORDER BY system.cs_no

我得到结果:

event_date  zone_id cs_no   event_id    descr
2019-01-22 07:10:10.607 9   V0137C4 7381    SENSOR
2019-01-22 07:10:12.637 10  V0137C4 7381    SENSOR
2019-01-23 08:44:50.737 9   V0137C4 7381    SENSOR
2019-01-23 08:44:50.907 10  V0137C4 7381    SENSOR
2019-01-23 21:01:09.523 9   V0137C4 7381    SENSOR
2019-01-23 21:01:09.540 10  V0137C4 7381    SENSOR
2019-01-24 06:58:44.107 9   V0137C4 7381    SENSOR
2019-01-24 06:58:46.137 10  V0137C4 7381    SENSOR
2019-01-25 07:24:07.000 9   V0137C4 7381    SENSOR
2019-01-25 07:24:07.080 10  V0137C4 7381    SENSOR
2019-01-28 06:54:53.507 9   V0137C4 7381    SENSOR
2019-01-28 06:54:53.600 10  V0137C4 7381    SENSOR
2019-01-29 07:45:10.520 9   V0137C4 7381    SENSOR
2019-01-29 07:45:12.597 10  V0137C4 7381    SENSOR

使用以下查询:

 WITH A AS (SELECT     MAX(evhist_XPK.event_date) AS maxdate, system.cs_no, evhist_XPK.event_id, evhist_XPK.zone_id, event.descr, ROW_NUMBER() OVER (PARTITION BY system.cs_no ORDER BY evhist_XPK.event_date DESC) AS rn
    FROM         evhist_XPK INNER JOIN
                 system ON evhist_XPK.system_no = system.system_no INNER JOIN
                 site ON system.site_no = site.site_no INNER JOIN
                 event ON event.event_id=evhist_XPK.event_id
    WHERE      (evhist_XPK.event_id = '7381')  AND (system.systype_id like 'V%') AND (evhist_XPK.event_date >= DATEADD(day, DATEDIFF(day, 7, GETDATE()), 0)) 
    AND (site.sitestat_id IN('A','PF'))
    GROUP BY system.system_no, evhist_XPK.event_date, system.cs_no, evhist_XPK.event_id, evhist_XPK.zone_id, event.descr),

    b AS (SELECT     MAX(evhist_XPK.event_date) AS maxdate, system.cs_no, evhist_XPK.event_id, evhist_XPK.zone_id,event.descr, ROW_NUMBER() OVER (PARTITION BY system.cs_no ORDER BY evhist_XPK.event_date DESC) AS rn
    FROM         evhist_XPK INNER JOIN
                 system ON evhist_XPK.system_no = system.system_no INNER JOIN
                 site ON system.site_no = site.site_no INNER JOIN
                 event ON event.event_id=evhist_XPK.event_id
    WHERE        (evhist_XPK.event_id = '7381')  AND (system.systype_id like 'V%')AND (evhist_XPK.event_date >= DATEADD(day, DATEDIFF(day, 7, GETDATE()), 0)) AND (site.sitestat_id IN('A','PF'))
    GROUP BY system.system_no, evhist_XPK.event_date, system.cs_no, evhist_XPK.event_id, evhist_XPK.zone_id, event.descr)

    Select A.maxdate as Fecha, A.cs_no as abonado, A.event_id as evento, A.descr, A.zone_id from A LEFT JOIN B on A.cs_no=B.cs_no where A.rn=1 and B.rn=1
    order by a.cs_no

我明白了:

2019-01-29 07:45:12.597 10  V0137C4 7381    SENSOR

但是我也需要下一个:

2019-01-29 07:45:10.520 9   V0137C4 7381    SENSOR

可以请教吗?

1 个答案:

答案 0 :(得分:0)

您不只是使用窗口功能吗?

SELECT x.*
FROM (SELECT eh.event_date, eh.zone_id, s.cs_no, eh.event_id, e.descr,
             MAX(eh.event_date) OVER (PARTITION BY eh.zone_id) as max_event_date
      FROM evhist_XPK eh INNER JOIN
           system s
           ON eh.system_no = s.system_no INNER JOIN
           site si
           ON s.site_no = si.site_no INNER JOIN
           event e
           ON e.event_id = eh.event_id
      WHERE eh.event_id = '7381'  AND s.systype_id like 'V%' AND
            eh.event_date >= DATEADD(day, DATEDIFF(day, 7, GETDATE()), 0)) AND  
            si.sitestat_id IN( 'A', 'PF')
     ) x
WHERE event_date = max_event_date
ORDER BY s.cs_no;