Skip to content
Advertisement

Oracle: How to get first returned record and previous record

I want to grab the first returned record and previous record based on a user input date.

Example, I put in 7/5/22, and I want to grab that date’s food value and somehow include the record previous to that, so output would be:

If I put in 3/16/22, then I want my output to be:

My totally wrong code:

Advertisement

Answer

Simply select the rows where the date is equal to or less than the decided date, read descending, fetch 2 rows only.

Note: Date is an Oracle reserved word (https://en.wikipedia.org/wiki/SQL_reserved_words), so it needs to be delimited as "Date".

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement