Reputation: 17
i have one table called products and the other one called prices.
Now i am trying to to show all products with and without prices, so i am using a left join. So far so good.
select * from products,prices where products.id = prices.id(+)
The prices table does also have a validFrom and a validTo record. What i want to achieve now is to exclude the expired records(first two rows). Is this possible when i am using left join
this is the result of the query.
ResultSet:
With
SELECT *
FROM products
LEFT JOIN prices
ON products.id = prices.id
AND (prices.validfrom >= sysdate
OR prices.validfrom IS NULL)
AND (prices.validto < sysdate
OR prices.validto IS NULL);
I get
expected result however should be:
Upvotes: 0
Views: 306
Reputation: 478
Try this:
SELECT * FROM (select * from products,prices where products.id = prices.id(+)) WHERE (prices.validTo IS NULL OR prices.validTo > sysdate)
Upvotes: 0
Reputation: 37482
Add a check, that prices.validfrom <= sysdate AND prices.validto > sysdate
to the ON
clause.
SELECT *
FROM products
LEFT JOIN prices
ON products.id = prices.id
AND (prices.validfrom <= sysdate
OR prices.validfrom IS NULL)
AND (prices.validto > sysdate
OR prices.validto IS NULL);
You should also use the explicit LEFT JOIN
syntax instead of that ancient WHERE ... (+)
thing.
Upvotes: 1