2017-08-23 167 views
0

我有這個疑問:獲取最新記錄每個TIMESTAMP

SELECT TIMESTAMP, 
     TIMESTAMP_OFFER, 
     TIMESTAMP_TRACK, 
     STATUS_ACPT 
FROM TABLE1 
WHERE TIMESTAMP IN 
(
    SELECT TIMESTAMP 
    FROM TABLE2 
    WHERE STATUS = '1' 
    AND TIMESTAMP_PROCESSED BETWEEN 
      '2017-01-07-00.00.00.000000' 
     AND '2017-01-17-00.00.00.000000' 
) 

而且結果:

--------------------------------------------------------------------------------------------------------- 
    | TIMESTAMP     | TIMESTAMP_OFFER    | TIMESTAMP_TRACK   | STATUS_ACPT | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-09 11:26:29.434254 | 2017-01-25 14:36:23.587199 | 2017-01-25 14:36:23.587199 | P   | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-09 11:26:29.434254 | 2017-02-01 16:05:02.221179 | 2017-02-01 16:05:02.221179 | P   | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-09 11:26:29.434254 | 2017-02-01 16:05:02.221179 | 2017-07-13 10:59:20.342584 | E   | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-11 15:09:51.935322 | 2017-02-11 12:00:50.410959 | 2017-02-11 12:00:50.410959 | P   | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-11 15:09:51.935322 | 2017-02-11 12:00:50.410959 | 2017-02-11 12:10:50.410959 | A   | 
    --------------------------------------------------------------------------------------------------------- 

我需要最新的記錄每一個時間戳,我應該得到這樣的:

--------------------------------------------------------------------------------------------------------- 
    | TIMESTAMP     | TIMESTAMP_OFFER    | TIMESTAMP_TRACK   | STATUS_ACPT | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-09 11:26:29.434254 | 2017-02-01 16:05:02.221179 | 2017-07-13 10:59:20.342584 | E   | 
    --------------------------------------------------------------------------------------------------------- 
    |2017-01-11 15:09:51.935322 | 2017-02-11 12:00:50.410959 | 2017-02-11 12:10:50.410959 | A   | 
    --------------------------------------------------------------------------------------------------------- 

我如何得到它?

如您所見,TIMESTAMP和TIMESTAMP_OFFER可以具有重複值,但TIMESTAMP_TRACK是唯一的。

+0

你必須給我們更多的細節!目前還不清楚爲什麼你期望結果集顯示爲具有重複時間戳的其他行也可以符合條件 - 那麼完整的業務需求是什麼? – MichaelTiefenbacher

+0

@MichaelTiefenbacher的要求是根據最新的TIMESTAMP_OFFER和TIMESTAMP_TRACK獲得給定TIMESTAMP的結果。因爲TIMESTAMP_TRACK是唯一的,所以我應該只能得到一個(最新)記錄。 – Foxy

+0

1)經典的例子[標籤:最大的每組]的問題。 2)[不要使用'BETWEEN'](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/what-do-between-and-the-devil-have-in-common。 aspx) - 你選擇1/17的第一個瞬間,這幾乎肯定不是你想要的(是的,這篇文章是爲SQL Server編寫的,但推理是普遍的 - 還記得一些DB2版本允許你指定小數秒)。 –

回答

0

假設表1是你上面所示的結果表可以與此查詢解決它:

with temp as (
select timestamp, timestamp_offer, timestamp_track, status_acpt, 
     row_number() over (order by timestamp desc) as rownum_timestamp, 
     row_number() over (order by timestamp_offer desc) as rownum_timestamp_offer 
    from table1) 
    select timestamp, timestamp_offer, timestamp_track 
    from temp 
    where rownum_timestamp = 1 or rownum_timestamp_offer = 1 

公共表表達式是用於添加rownumbers - 一個爲你和需要的最大的每個時間戳列主要的SQL語句只檢索rownumber = 1的行。請注意排序順序 - 它正在遞減,所以rownumber 1是最大時間戳。