I have the following widgets
table in Postgres:
(That screenshot is a crude Excel representation of it.) I am trying to write a single SQL query that will return me:
- The
widgets
record representing the widget with today's earliest load time; or - If no
widgets
were loaded today, the one with the earliest load time ever (for the whole table)
So, using the image above:
- The query would first try to return the widget which was loaded first, today (if such a widget exists). In this case, only widgets with an id of 3 and 5003094 (respectively) were loaded today. Of these two,
widget_id = 3
was loaded earlier than the other, so this is the record that the query would return. - However, if we pretend that those two widgets were not in the table, and furthermore, that no widgets were loaded today, then the query would return
widget_id = 1
, because it was loaded back in 2010.
Here's my initial attempt at the query:
SELECT
MIN(w.loaded_date_time)
FROM
widgets w
WHERE
w.loaded_date_time >= now()
OR
1=1
However, I know right off the bat that this is not syntactically correct. Any ideas? Thanks in advance!