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是唯一的。
你必須給我們更多的細節!目前還不清楚爲什麼你期望結果集顯示爲具有重複時間戳的其他行也可以符合條件 - 那麼完整的業務需求是什麼? – MichaelTiefenbacher
@MichaelTiefenbacher的要求是根據最新的TIMESTAMP_OFFER和TIMESTAMP_TRACK獲得給定TIMESTAMP的結果。因爲TIMESTAMP_TRACK是唯一的,所以我應該只能得到一個(最新)記錄。 – Foxy
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版本允許你指定小數秒)。 –