I have this two tables:
mysql> desc vat_rates;
+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| rate_id | varchar(5) | NO | PRI | NULL | |
| name | varchar(255) | NO | | NULL | |
| type | enum('O','I') | NO | | NULL | |
| default | tinyint(1) | YES | | 0 | |
+-------------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> desc vat_rates_details;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| rate_id | varchar(10) | NO | | NULL | |
| effect_date | date | NO | | NULL | |
| rate | float | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
I need a query that gives me vat_rates.name, vat_rates.rate_id, vat_rates_details.rate WHERE the effect_date is MAX() but <= than now().
Simplifying, I need to select all vat_rates with rate field being the last date based on effect_date but not in the future.
Example with data:
vat_rates:
rate_id = ‘L9’
name = ‘MyVatName’
type = ‘O’
default = 1
vat_rates_details (line 1):
rate_id = ‘L9’
effect_date = ‘2000-01-01’
rate = 20
vat_rates_details (line 2):
rate_id = ‘L9’
effect_date = ‘2010-06-01’
rate = 19
vat_rates_details (line 3):
rate_id = ‘L9’
effect_date = ‘2010-07-01’
rate = 21
The expected result:
+-------------+-------------+------+ | rate_id | name | rate | +-------------+-------------+------+ | L9 | MyVatName | 19 | +-------------+-------------+------+
Thanks for your time 🙂
EDIT: Expected rate is 19 not 20. Tks Justin.
1 Answer