2017-03-08 94 views
1

,我有以下表中的數據:返回單個最新記錄每一個設備ID表

+-----+---------------------+---------------------+------------+--------+------+-----------------+ 
    | id | date_timer_off  | date_timer_on  | event_done | device | user | admin_email  | 
    +-----+---------------------+---------------------+------------+--------+------+-----------------+ 
    | 145 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T   |  3 | 4 | [email protected] | 
    | 146 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T   |  4 | 4 | [email protected] | 
    | 147 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T   |  5 | 4 | [email protected] | 
    | 148 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T   |  3 | 4 | [email protected] | 
    | 149 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T   |  4 | 4 | [email protected] | 
    | 150 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T   |  5 | 4 | [email protected] | 
    | 151 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T   |  3 | 4 | [email protected] | 
    | 152 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T   |  4 | 4 | [email protected] | 
    | 153 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T   |  5 | 4 | [email protected] | 
    | 154 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F   |  3 | 4 | [email protected] | 
    | 155 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F   |  4 | 4 | [email protected] | 
    | 156 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F   |  5 | 4 | [email protected] | 
    | 157 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F   |  3 | 4 | [email protected] | 
    | 158 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F   |  4 | 4 | [email protected] | 
    | 159 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F   |  5 | 4 | [email protected] | 
    | 160 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F   |  3 | 4 | [email protected] | 
    | 161 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F   |  4 | 4 | [email protected] | 
    | 162 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F   |  5 | 4 | [email protected] | 
    +-----+---------------------+---------------------+------------+--------+------+-----------------+ 

我需要的是如下的結果:

+-----+---------------------+---------------------+------------+--------+------+-----------------+ 
    | id | date_timer_off  | date_timer_on  | event_done | device | user | admin_email  | 
    +-----+---------------------+---------------------+------------+--------+------+-----------------+ 
    | 160 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F   |  3 | 4 | [email protected] | 
    | 161 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F   |  4 | 4 | [email protected] | 
    | 162 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F   |  5 | 4 | [email protected] | 
    +-----+---------------------+---------------------+------------+--------+------+-----------------+ 

它需要只返回表格中每個設備ID的最新一條記錄,並且僅在event_done狀態爲'F'或換句話說爲false時才返回。

記錄還必須處於正在進行請求的當前分鐘,因爲數據庫將每20秒查詢一次以更新定時器列表。

例如,如果請求是在19:02:20進行的,那麼應該有一個檢查來檢查date_timer_off或date_timer_on字段是否在請求發出的分鐘內(19:02:20),從而返回以上結果集。

我試圖從這篇文章中實現答案,SQL query for returning the latest record for each ID,但沒有成功。

這是當前在生產環境中使用的查詢,但它會導致問題,例如下一個事件在應該時不會觸發。

select * from timers_data data where data.date_timer_off >= :today AND data.date_timer_off <= :tomorrow AND data.date_timer_on >= :today AND data.date_timer_on <= :tomorrow AND data.event_done = 'F' ORDER BY data.id DESC 

我也試過這樣:

select * from timers_data where event_done = 'F' and (date_timer_off >= now() or date_timer_on >= now()) ORDER BY id desc; 

在MySQL上運行它的時候,卻似乎有一個問題,當我在使用Spring的JPA CrudRepository下面我的Spring應用程序運行它,它的工作原理:

@Query(value = "select * from timers_data where event_done = 'f' and (date_timer_off >= now() or date_timer_on >= now()) ORDER BY id desc;", nativeQuery = true) 
    ArrayList<TimersData> findTimersForToday(); 

這是我目前在我的項目中使用:

@Query(value = "select * from timers_data data where data.date_timer_off >= :today AND data.date_timer_off <= :tomorrow AND data.date_timer_on >= :today AND data.date_timer_on <= :tomorrow AND data.event_done = 'F' ORDER BY data.id DESC", nativeQuery = true) 
    ArrayList<TimersData> findTimersForToday(@Param("today") Date today, @Param("tomorrow") Date tomorrow); 

任何幫助將不勝感激。

在此先感謝

+0

「的記錄也必須在當前分鐘請求正在被提出「並且如果沒有這樣的記錄沒有記錄被返回?爲什麼這不是一個簡單的'date_timer_off和date_timer_on和event_Done ='F''之間的WHERE sysdate()?它假定你不能有定時器重疊的設備,但是基於似乎合理的問題。 – xQbert

+0

在閱讀您嘗試過的內容之後...在第一個datetime_off ...中將您的OR更改爲並將您的> =改爲<= ... select * from timers_data where event_done ='F'and(date_timer_off <= now()AND date_timer_on> = now())ORDER BY id desc;' – xQbert

+0

謝謝@xQbert,如果解決方案一直存在問題,也會嘗試。 – Barend

回答

0

,你可以在條款基礎上匹配設備和最大的是元組使用子查詢與(日期..)

select * from timers_data data 
where (device, date_timer_off) in ( 
    select device, max(date_timer_off) 
    from timers_data data 
    where data.date_timer_off >= :today 
    AND data.date_timer_off <= :tomorrow 
    AND data.date_timer_on >= :today 
    AND data.date_timer_on <= :tomorrow 
    AND data.event_done = 'F' 
    group by device) 
ORDER BY data.id 
+0

It Works!至今。感謝您的及時迴應。 – Barend