Here's the data in the invoicehistory table.
invoicehistoryID WorkOrderNumber InvoiceDate
1 1 2017-06-13 12:00:49
2 1 2017-06-13 12:05:05
3 10 2017-06-14 12:32:59
4 10 2017-06-14 14:26:25
5 1 2017-06-14 14:26:54
6 1 2017-06-22 10:12:49
What SELECT query do I need to get only the latest InvoiceDate for each WorkOrderNumber, ordered by WorkOrderNumber? In other words, how can I get this:
invoicehistoryID WorkOrderNumber InvoiceDate
6 1 2017-06-22 10:12:49
4 10 2017-06-14 14:26:25
I haven't had success with DISTINCT. I haven't had success with LIMIT because my invoicehistory table will grow.
This almost works,
SELECT max(InvoiceDate),WorkOrderNumber FROM `invoicehistory` GROUP BY WorkOrderNumber
but doesn't get the invoicehistoryID.
This
SELECT max(InvoiceDate),WorkOrderNumber, invoicehistoryID FROM `invoicehistory` GROUP BY WorkOrderNumber, invoicehistoryID
returns all of the rows.