I was wondering if it was possible to run a IN condition and a > condition (only if not found in the IN) at the same time when doing date comparisons from a row of dates?
For instance if 2018-01-01 doesn't exist it will pull the next available row of the date 2018-01-02 and it should do this for a random array of dates I supply. (NOT RANGE since this will pull in dates I'm not interested in.)
Example:
create table trade (
id serial primary key,
year int,
month int,
"data" json
);
insert into trade ("year", "month", "data") VALUES (
2018, 1, '{"2": {"low": 19, "high": 21, "open": 20, "close": 20, "volume": 1000}, "3": {"low": 19, "high": 21, "open": 20, "close": 20, "volume": 1000}}'::json
);
insert into trade ("year", "month", "data") VALUES (
2018, 2, '{"1": {"low": 19, "high": 21, "open": 20, "close": 20, "volume": 1000}, "2": {"low": 19, "high": 21, "open": 20, "close": 20, "volume": 1000}}'::json
SELECT
t.prices,
make_date("year", "month", t.day::int) as date
FROM
trade
JOIN json_each(trade.data) t(day, prices) ON TRUE
WHERE
make_date("year", "month", t.day::int) IN ('2018-01-1', '2018-01-03')
);
Would like it to return prices of 1950-01-03, 2018-01-03, AND 2018-01-02 (since 2018-01-01 doesn't exist)
I'm working on a function that will do a 1/1 ratio of results when I supply it dates I'm interested in, and if they don't exist, it will return the next available date.