I try to compare a date with dates which are in different rows. I hope the picture makes it clear.
For example:
The Product with ID "2" in "June" should have a fee of 100$ because the fee was valid from April to July.
I try to compare a date with dates which are in different rows. I hope the picture makes it clear.
For example:
The Product with ID "2" in "June" should have a fee of 100$ because the fee was valid from April to July.
For a single given product_id
:
SELECT * -- which columns do you need?
FROM product p
JOIN productfee f ON f.valid_from <= p.date
WHERE p.product_id = 2
ORDER BY f.valid_from DESC
LIMIT 1;
If tables are of non-trivial size, you'd better have at least these indexes:
CREATE INDEX ON product (product_id);
CREATE INDEX ON productfee (valid_from DESC);
(Or more specialize, depending on the complete picture.)
If valid_from
can be NULL
, consider NULLS LAST
. See:
Obviously, you'll use proper date
or timestamp
types, not just month names.