Dear Community,
I created a table "Product" (simplified) with the following fields: "ProductName", "Amount", "lastUpdate".
I was able to build a query showing me always the latest Record for each Product (based on the field "lastUpdate").
Now I would like to create a query showing me the last valid record for each month / year. The rule should be: always take the latest record available up until end of the specific month. If there are newer records, which have an update date after end of the month, then don't use this record, but the previous record for this product.
Example:
The following table
ProductName.......Amount.......lastUpdate
P1..........................5...............20-Feb-2015
P2........................10...............10-Mar-2015
P1........................10...............01-May-2015
Should bring the following result :
Month........ProductName........Amount
January.......P1.........................0
February.....P1.........................5
March........P1..........................5
April...........P1..........................5
May...........P1........................10
June...........P1.........................10
... ... ...
January......P2..........................0
February....P2..........................0
March........P2........................10
April..........P2.........................10
...
I was thinking about creating a second table with called "Period" (simplified) with the fields "Month" and "LastDateOfMonth"
And then building a query, linking both this tables with a were condition.
In a not proper SQL syntax I am thinking about something like:
Select Max("LastUpdate") and "ProductName" and "Amount" from table "Product" where "lastUpdate" <= "Period"."LastDateOfMonth" for each record in table "Period".
Please excuse this really bad syntax, as I normal use the query build to build my queries.
Any ideas on how to build this query, or any other idea on how to solve this? Your help would be very much appreciated.
I am using Access 2007
I created a table "Product" (simplified) with the following fields: "ProductName", "Amount", "lastUpdate".
I was able to build a query showing me always the latest Record for each Product (based on the field "lastUpdate").
Now I would like to create a query showing me the last valid record for each month / year. The rule should be: always take the latest record available up until end of the specific month. If there are newer records, which have an update date after end of the month, then don't use this record, but the previous record for this product.
Example:
The following table
ProductName.......Amount.......lastUpdate
P1..........................5...............20-Feb-2015
P2........................10...............10-Mar-2015
P1........................10...............01-May-2015
Should bring the following result :
Month........ProductName........Amount
January.......P1.........................0
February.....P1.........................5
March........P1..........................5
April...........P1..........................5
May...........P1........................10
June...........P1.........................10
... ... ...
January......P2..........................0
February....P2..........................0
March........P2........................10
April..........P2.........................10
...
I was thinking about creating a second table with called "Period" (simplified) with the fields "Month" and "LastDateOfMonth"
And then building a query, linking both this tables with a were condition.
In a not proper SQL syntax I am thinking about something like:
Select Max("LastUpdate") and "ProductName" and "Amount" from table "Product" where "lastUpdate" <= "Period"."LastDateOfMonth" for each record in table "Period".
Please excuse this really bad syntax, as I normal use the query build to build my queries.
Any ideas on how to build this query, or any other idea on how to solve this? Your help would be very much appreciated.
I am using Access 2007
Last edited: