CREATE TABLE logistics ( id SERIAL PRIMARY KEY, time_stamp DATE, quantity INT ); INSERT INTO logistics (time_stamp, quantity) VALUES ('2020-01-14', '100'), ('2020-01-15', '400'), ('2020-01-16', '530'), ('2020-01-17', '500');
Expected Result
time_stamp | quantity | ------------|------------|------ 2020-01-15 | 400 | 2020-01-16 | 530 | 2020-01-17 | 500 |
In the results I want to display the quantity
for all dates except for the first row.
I know in the example above I can do this with a simple WHERE-condition
like this:
SELECT l.time_stamp AS time_stamp, SUM(quantity) AS quantity FROM logistics l WHERE time_stamp <> '2020-01-14' GROUP BY 1 ORDER BY 1;
However, it is neither guaranteed
that the first time_stamp
will be always 2020-01-14
nor that the MIN(2020-01-14)
is always in the first row.
Therefore, I am wondering how I have to modify the query to always not-display the first row of the results?
Advertisement
Answer
Since this is Postgresql, you can use the OFFSET clause.
https://www.postgresql.org/docs/current/queries-limit.html
SELECT l.time_stamp AS time_stamp, SUM(quantity) AS quantity FROM logistics l GROUP BY 1 ORDER BY 1 OFFSET 1;