获取每个TIMESTAMP的最新记录

时间:2017-08-23 14:49:41

标签: sql select db2 sql-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                  | 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是唯一的。

1 个答案:

答案 0 :(得分:0)

假设table1是上面显示的结果表,您可以使用此查询解决它:

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是最大时间戳。