2013-07-31 119 views
0

EX:檢索最新的日期

ID  Date(with time)   Price 
    ----  -------    ------ 
    A   23-Aug-12 (09:25pm)  10(cosider this was the latest on this date) 
    A   25-May-10    20 
    A   23-Aug-12 (8:20pm)  30 
    A   23-Aug-12 (7:00pm)  35 
    B   03-Apr-09    45 
    B   05-Dec-10    60 

我想找回ID,日期,價格即如果兩個相同的日期,如果德多個價格的話,我必須選擇是最新的日期根據包含時間戳的日期更新。

預期輸出:

   A,23-Aug,12,10 
       A,25-May-10,20 
       B,03-Apr-09,45 
       B,05-Dec-10,60 

回答

0

大多數SQL版本都支持row_number功能。從日期時間提取日期因數據庫而異。這裏有一種方法可以做你想做的事:

select id, datetime, price 
from (select t.*, 
      row_number() over (partition by id, cast(datetime as date) order by datetime desc 
           ) as seqnum 
     from t 
    ) t 
where seqnum = 1; 

這給出了一般結構。確切的語法因數據庫而異。

0

試試這個: -

SELECT 
    ID, 

    MAX(date) 
FROM 
    Some_Unnamed_Table 
GROUP BY 
    ID 
ORDER BY 
    ID