I have an invoicing system and am trying to generate reports on hours spent. I'm saving every instance of a change to the order, so there are multiple entries for almost every item on every invoice. Due to this, I'm filtering out the old changes and am trying to only use the most recent.
Each instance sharing a project_id, phase_id, and the same weekstart are the same item on the invoice. I want to generate a report and only grab the most recent versions of those items.
Example table:
id project_id phase_id weekstart created
---------------------------------------------------------------
1 6 apple 2017-04-20 2017-04-23
2 6 apple 2017-04-20 2017-04-24
3 8 banana 2017-04-20 2017-04-23
4 9 pear 2017-04-20 2017-04-23
5 9 pear 2017-04-20 2017-04-25
I want to be able to run a query to get:
id project_id phase_id weekstart created
---------------------------------------------------------------
2 6 apple 2017-04-20 2017-04-24
3 8 banana 2017-04-20 2017-04-23
5 9 pear 2017-04-20 2017-04-25
Currently I'm using something like:
SELECT * from invoiceitems where employee_id = 10
group by project_id, phase_id, weekstart
But this doesn't account for the creation date.
Ordering the results doesn't have any affect on the group by statement. I've checked for similar posts, but only two I found are looking to order by the highest creation date altogether or aren't grouping the results by multiple columns.