How can PostgreSQL return a list of the oldest timestamp values over a table of sensor id measurements?
Let me explain the situation with a sample table:
CREATE TABLE sensor_data(
sensor_id INTEGER,
time TIMESTAMPTZ,
value NUMERIC,
PRIMARY KEY (sensor_id, time)
)
Populated table example:
+-----------+------------------+-------+
| sensor_id | time | value |
+-----------+------------------+-------+
| 1 | 2018-01-01 00:00 | 1 |
| 1 | 2018-01-01 01:00 | 2 |
| 3 | 2018-01-01 03:00 | 4 |
| 3 | 2018-01-01 04:00 | 3 |
| 4 | 2018-01-01 03:00 | 5 |
| 4 | 2018-01-01 04:00 | 6 |
+-----------+------------------+-------+
While using something like sensor_id (1,3) inside the query I want it to return something like this:
+-----------+------------------+-------+
| sensor_id | time | value |
+-----------+------------------+-------+
| 1 | 2018-01-01 01:00 | 2 |
| 3 | 2018-01-01 04:00 | 3 |
+-----------+------------------+-------+
How can I do that in a query using the PRIMARY KEY
index for speeding it up?