Ian writes "I have a table of orders and a table of tax rates, where the tax rate can change over time. I want to write a select statement that will return one row per order, showing the tax rate that was valid on the order date.I think I need a subquery to find the correct row from tax_rates but I can't find how to include the order_date in the subqueryExample dataTable - tax_rates product_id country effective_date tax_ratePR01 UK 01-Jan-04 10%PR01 UK 31-Dec-04 17%PR01 UK 17-Feb-05 22%PR01 UK 01-Apr-05 15%PR01 JP 01-Jan-04 35%
Table – orders order_no order_date product_id country1 01-Jan-05 PR01 UK2 28-Feb-05 PR01 UK3 02-Mar-05 PR01 UK4 14-Feb-05 PR01 JP
We can join the tables on product_id and country, but then each order may join to several rows with different effective_dates.The correct row to return is the one with the *highest effective date that is <= order_date*Required result order_no order_date product_id country tax_rate1 01-Jan-05 PR01 UK 17%2 28-Feb-05 PR01 UK 22%3 02-Mar-05 PR01 UK 22%4 14-Feb-05 PR01 JP 35%
"