简体   繁体   中英

sql select statement by choosing min timestamp value

i'm having data as below in table

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      

I want select query statement which will select data for each serial number with min timestamp value for selected date,

so the expected result when selection date is 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      

how this can be done, i try to use min(TIMESTAMP) but get err 'An unexpected token "END-OF-STATEMENT"'

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM