sql - First value in a aggregated query -


i have table statistical values process. table has following format:

create table data (     process    integer   not null,     time       timestamp not null     first      double precision,     last       double precision,     first_time timestamp,     last_time  timestamp ) 

the data in table inserted every minute, , contains aggregate value of last minute. example, process 1, can have following data:

+---------------------------------------------------------------------------------+ |             process | time | first | last  | first_time | last_time             | +---------------------------------------------------------------------------------+ | 1 | 2014-09-22 12:00:00 | 100 | 200 | 2014-09-22 12:00:00 | 2014-09-22 12:00:59 | | 1 | 2014-09-22 12:01:00 | 104 | 152 | 2014-09-22 12:01:00 | 2014-09-22 12:01:59 | | 1 | 2014-09-22 12:02:00 | 141 | 155 | 2014-09-22 12:02:10 | 2014-09-22 12:02:59 | | 1 | 2014-09-22 12:03:00 | 122 | 147 | 2014-09-22 12:03:00 | 2014-09-22 12:02:45 | +---------------------------------------------------------------------------------+ 

as can see in third row, there times when first value not second 0 of minute. in last time happen (fourth row).

using first , last functions this page, , date_round function this page, want select first value of given process in 30 minute interval.

when try following 2 queries, both result correct.

select     date_round(time, '30 min'::interval) "time",     first(first)     data     process = 1     ,     time > '2014-09-20 00:00:00'     ,     time < '2014-09-22 18:00:00' group 1 order 1 

and

select     date_round(time, '30 min'::interval) "time",     first(first) "value" (     select         time,         first             data             process = 1         ,         time > '2014-09-20 00:00:00'         ,         time < '2014-09-22 18:00:00'     order 1 ) group 1 order 1 

my question is: why first query works in case? postgres sort data before apply group clause?

my doubt because "first" function returns first value of 2 passed arguments. if data not sorted, first call in "first" aggregate function can contain value not in correct order, resulting in invalid value, right?

can use first query safely or should use second query?

this query want:

select distinct on (date_round(time, '30 min'::interval))        date_round(time, '30 min'::interval) "time",        first data process = 1 ,       time > '2014-09-20 00:00:00' ,       time < '2014-09-22 18:00:00' order date_round(time, '30 min'::interval), time; 

postgres doesn't ordering before group by -- no database definition of sql. happens encounter earliest record first, there no guarantee. in fact, don't think second version guaranteed ordered either (although cannot find explicit postgres documentation on point).


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -