jdbc - find minimum value by grouping the timestamp in sql -
how find minimum value following set or records in sql
date values ------------------------- ------ 2013-07-20 18:05:38.599 257 2013-07-20 18:06:39.264 118 2013-07-20 18:07:39.51 142 2013-07-20 18:08:39.67 138 2013-07-20 18:09:40.279 123 2013-07-20 18:10:40.697 115 2013-07-20 18:11:41.158 124 2013-07-20 18:12:41.327 128 2013-07-20 18:13:41.797 533 2013-07-20 18:14:42.345 129 2013-07-20 18:15:42.694 124 2013-07-20 18:16:43.222 114 2013-07-20 18:17:43.715 119 2013-07-20 18:18:44.162 126
i need find minimum value every 3 min of timestamp. can please suggest query work this??
always specify dbms, , provide create table , insert statements in question.
create table test ( date timestamp, value integer ); insert test values ('2013-07-20 18:05:38.599', 257), ('2013-07-20 18:06:39.264', 118), ('2013-07-20 18:07:39.51', 142), ('2013-07-20 18:08:39.67', 138), ('2013-07-20 18:09:40.279', 123), ('2013-07-20 18:10:40.697', 115), ('2013-07-20 18:11:41.158', 124), ('2013-07-20 18:12:41.327', 128), ('2013-07-20 18:13:41.797', 533), ('2013-07-20 18:14:42.345', 129), ('2013-07-20 18:15:42.694', 124), ('2013-07-20 18:16:43.222', 114), ('2013-07-20 18:17:43.715', 119), ('2013-07-20 18:18:44.162', 126);
use platform's date/time functions generate virtual table of "buckets", each 1 containing interval of 3 minutes. exactly how varies among sql platforms, because date/time functions vary lot among sql platforms. i'm using postgresql, i'm going use generate_series() function in common table expression.
with buckets ( select n start_time, n + interval '3' minute end_time generate_series(timestamp '2013-07-20 18:05:38.599', timestamp '2013-07-20 18:18:44.162', '3 minutes') n ) select min(test.value), start_time, end_time test inner join buckets on test.date >= start_time , test.date < end_time group start_time, end_time order start_time; 118 2013-07-20 18:05:38.599 2013-07-20 18:08:38.599 115 2013-07-20 18:08:38.599 2013-07-20 18:11:38.599 124 2013-07-20 18:11:38.599 2013-07-20 18:14:38.599 114 2013-07-20 18:14:38.599 2013-07-20 18:17:38.599 119 2013-07-20 18:17:38.599 2013-07-20 18:20:38.599
Comments
Post a Comment