php - SQL find rows with closest higher values than each value in specified set of values -
i have statistics gathered each 6 hours in database each saved timestamp. have array of timestamp in code. need select value database each value in array of timestamp , row have closest higher or equal timestamp in array.
to illustrate: table data
id timestamp value 1 1400000027 10 2 1400000035 15 3 1400000043 20 4 1400000044 21 5 1400000048 30 6 1400000060 35
the array contains following timestamps:
[1400000020, 1400000024, 1400000035, 1400000050]
the rows need database based on input array are:
id timestamp value 1 1400000027 10 1 1400000027 10 2 1400000035 15 6 1400000060 35
is there simple way in 1 query? , best solution in doctrine, since using symfony 2 , doctrine.
this typically done distinct on
in postgresql (if can use non-standard sql)
select distinct on (ts_min) t.* unnest(array[1400000020, 1400000024, 1400000035, 1400000050]) ts_min left join table_name t on t.timestamp >= ts_min order ts_min, t.timestamp
if cannot bind array, can use values
construct:
from (values (1400000020), (1400000024), (1400000035), (1400000050)) v(ts_min)
related solutions:
Comments
Post a Comment