Hello All,
I have an audit table that tracks changes to 'transit routes'. The table is of the order:
Name, ChangeType, ChangeDate
CR01A, New, 11/5/2015
CR01AP, New, 11/5/2015
CR01AP, Chg, 11/7/2015
CR01AP, Chg, 11/8/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
CR02A, New, 11/5/2015
...
CR0#x, New, 11/5/2015
These three fields are a composite key. There is other data associated with these rows but for the purpose of this example I only need to show these three fields.
Now, my invoicing application cycles through a user specified date range, lets say 11/5 -> 11/8, for this example.
First, I want to load into a recordset all the trips with the name Like CR01% <= the current invoice date in the loop.
So Iteration 1 (11/5/2015) should return, in a recordset:
CR01A, New, 11/5/2015
CR01AP, New, 11/5/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
Iteration 2 (11/6/2015) should return, in a recordset:
CR01A, New, 11/5/2015
CR01AP, New, 11/5/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
Iteration 3 (11/7/2015) should return, in a recordset:
CR01A, New, 11/5/2015
CR01AP, Chg, 11/7/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
And so on...
Basically what we are doing here is determining if there is a newer entry to analyze for each iteration (day) of the invoice period, and return a set, in this case of route CR01, that is the newest version for that given day of the loop.
This is the code that I thought would work to accomplish this, but there is a caveat:
Set rsTemp = invoicing.OpenRecordset("SELECT * FROM tripslog WHERE [tripName] LIKE '" & shortTripName1 & "%' AND [lastUpdate] = (Select Max([lastUpdate]) from tripslog WHERE [lastUpdate] <= #" _
& invoiceDateArray(i) & "# AND [tripName] LIKE '" & shortTripName1 & "%')") 'find the most recent set of trips that have the same form 'cr01..' from trip table
(shortTripName1 in this case is CR01)
It works when all of the dates are the same, but on iteration 3 (11/7) it is returning just CR01AP into the recordset. This has to do with the subquery lastupdate = select max(lastupdate).
Perhaps just a simple MySQL query will not accomplish what I hope (to grab all the trips from the table with the same form 'CR01' that are most recent to the date in the invoiceDateArray on each iteration. Maybe I need to post process in the recordset? I'm kind of stumped.
I have an audit table that tracks changes to 'transit routes'. The table is of the order:
Name, ChangeType, ChangeDate
CR01A, New, 11/5/2015
CR01AP, New, 11/5/2015
CR01AP, Chg, 11/7/2015
CR01AP, Chg, 11/8/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
CR02A, New, 11/5/2015
...
CR0#x, New, 11/5/2015
These three fields are a composite key. There is other data associated with these rows but for the purpose of this example I only need to show these three fields.
Now, my invoicing application cycles through a user specified date range, lets say 11/5 -> 11/8, for this example.
First, I want to load into a recordset all the trips with the name Like CR01% <= the current invoice date in the loop.
So Iteration 1 (11/5/2015) should return, in a recordset:
CR01A, New, 11/5/2015
CR01AP, New, 11/5/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
Iteration 2 (11/6/2015) should return, in a recordset:
CR01A, New, 11/5/2015
CR01AP, New, 11/5/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
Iteration 3 (11/7/2015) should return, in a recordset:
CR01A, New, 11/5/2015
CR01AP, Chg, 11/7/2015
CR01B, New, 11/5/2015
CR01BP, New, 11/5/2015
And so on...
Basically what we are doing here is determining if there is a newer entry to analyze for each iteration (day) of the invoice period, and return a set, in this case of route CR01, that is the newest version for that given day of the loop.
This is the code that I thought would work to accomplish this, but there is a caveat:
Set rsTemp = invoicing.OpenRecordset("SELECT * FROM tripslog WHERE [tripName] LIKE '" & shortTripName1 & "%' AND [lastUpdate] = (Select Max([lastUpdate]) from tripslog WHERE [lastUpdate] <= #" _
& invoiceDateArray(i) & "# AND [tripName] LIKE '" & shortTripName1 & "%')") 'find the most recent set of trips that have the same form 'cr01..' from trip table
(shortTripName1 in this case is CR01)
It works when all of the dates are the same, but on iteration 3 (11/7) it is returning just CR01AP into the recordset. This has to do with the subquery lastupdate = select max(lastupdate).
Perhaps just a simple MySQL query will not accomplish what I hope (to grab all the trips from the table with the same form 'CR01' that are most recent to the date in the invoiceDateArray on each iteration. Maybe I need to post process in the recordset? I'm kind of stumped.