通过选择min timestamp值来选择sql语句

时间:2012-03-20 04:11:03

标签: sql db2

我的数据如表

所示
NUM_SER           STATUS     TIMESTAMP             
----------------  ---------  --------------------- 
00000000000000d1  02         2/21/2012 12:24:13 PM 
00000000000000d1  05         2/21/2012 5:49:46 PM  
00000000000000d2  01         2/21/2012 11:05:36 AM  
00000000000000d2  05         2/21/2012 10:27:57 PM  
00000000000000d2  03         2/21/2012 4:55:13 PM  
00000000000000d3  02         2/21/2012 3:18:23 PM  
00000000000000d3  05         2/21/2012 7:02:09 PM  
00000000000000d3  02         2/21/2012 7:16:03 PM      

我想选择查询语句,它将为每个序列号选择数据,并为所选日期选择最小时间戳值,

所以选择日期为2012-02-21时的预期结果

NUM_SER           STATUS     TIMESTAMP             
----------------  ---------  --------------------- 
00000000000000d1  02         2/21/2012 12:24:13 PM     
00000000000000d2  01         2/21/2012 11:05:36 AM   
00000000000000d3  02         2/21/2012 3:18:23 PM      

如何做到这一点,我尝试使用min(TIMESTAMP)但得到错误'意外的令牌“END-OF-STATEMENT”'

1 个答案:

答案 0 :(得分:3)

试试这个:

select * from t t1
left join t t2
on t1.num_ser = t2.num_ser and t1.timestamp > t2.timestamp
where t2.timestamp is null