I’ve got a table for a payroll system, with four fields, and some sample data:
pkref employee_id new_wage effective_date
===== =========== ======== ==============
23 06031-BOB 10 080101
37 06031-BOB 15 090501
90 06031-BOB 13 110228
When an employee’s wage is changed, a primary key reference auto-increments, and the appropriate information is recorded (effective_date is a timestamp, yymmdd). No problems there.
Now, I’m trying to get a query to find
- All entries related to an employee, then
- The maximum
effective_datestamp in those sub-entries - The wage that corresponds to that maximum.
I’ve made my very first subquery ever (!), got it almost right, but it’s buggy. Could some guru have a look and give me a bump in the right direction?
SELECT MAX(effective_date),new_wage FROM (SELECT effective_date,new_wage FROM hr_wages WHERE employee_code='06031-BOB') AS t1
Ideally, I want 110228 and 13 to be returned. But, as the aforementioned guru will no doubt see immediately, something is wrong. The new_wage value does not always match the max effective_date.
So. Vat to do?
Hang on, what’s stopping you from doing the following?