I have a SQL query:
select max(date)
from index_constituents
where (opening_closing ='O'
and index_code ='buk350n'
and issuer = 'cboe') and date = '2024-04-25';
id | date | issuer | index_code | opening_closing |
---|---|---|---|---|
1393 | 2024-04-25 | cboe | buk350n | O |
1394 | 2024-04-25 | cboe | buk350n | O |
When I run the same query but for the day before, I get:
```sql
select max(date)
from index_constituents
where (opening_closing ='O'
and index_code ='buk350n'
and issuer = 'cboe') and date = '2024-04-24';
```
id | date | issuer | index_code | opening_closing
- | - | - | - | -
1402 | 2024-04-24 | cboe | buk350n | O
When I run
```sql
select max(date)
from index_constituents
where (opening_closing ='O'
and index_code ='buk350n'
and issuer = 'cboe') and date = '2024-04-23';
```
NOTHING IS RETURNED.
So I want to return 2024-04-23
i.e. I want to find the most recent date BEFORE A GIVEN DATE where there is no data when I run my query
3
Answers
It’s working properly. May be you’re confusing about columns data type or values:
For check output :
Please check your column
date
&opening_closing
data_type.date
column maybevarchar
andopening_closing
may be boolean.Please check the table description:
If you need to find latest date with no record after existing record’s date then you can use 1st query:
If you need to find latest date before today with no records then use this second query:
This gets a list of distinct dates where the criteria are met. Each date is compared to the next earliest on the list to compute the gap starting with whatever is the latest date found. You want to find the day before the latest date where there’s a gap larger than one.
If no such gap exists within the data it will be treated as though occurring on the day before the earliest date found. If there are no rows meeting the criteria at all then no date is returned either.