Results 1 to 10 of 10
  1. #1
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51

    Most recent cost query

    I have a table with a history of the costs and a dates for each item. I want the most recent cost for each item.



    When I do a query and select the Max of date in the "Total" row, I get the most recent date for each cost. This means I have a new line each time the cost changed over the whole history.

    I think it may be possible to use a subquery in the "Criteria" row, but I haven't been able to figure out how.

    Here is the SQL I have now:
    SELECT [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO, [Material Cost Table 1].COST, Max([Material Cost Table 1].DATE) AS MaxOfDATE
    FROM [Material Cost Table 1]
    GROUP BY [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO, [Material Cost Table 1].COST;

    Thank you,

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    That looks like it is using two queries. Is there a way to make the first query a subquery? I tried to put the SQL from the first query into the "Criteria" row for the Date of the second query, but Access gave me an error saying my subquery can return more than 1 field and I don't have an Exists word in the From clause of the main query.

    First step SQL:

    (SELECT [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO, Max([Material Cost Table 1].DATE) AS MaxOfDATE
    FROM [Material Cost Table 1]
    GROUP BY [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO)

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Mine can be done as a subquery, though I don't like to do it that way:

    SELECT VehicleMiles.*
    FROM VehicleMiles INNER JOIN [SELECT Max([VehicleMiles].[DorDate]) AS MaxDate, [VehicleMiles].[CarNum]
    FROM VehicleMiles
    GROUP BY [VehicleMiles].[CarNum]]. AS qryMaxDates ON (VehicleMiles.CarNum = qryMaxDates.CarNum) AND (VehicleMiles.DorDate = qryMaxDates.MaxDate);
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    This is what I have and it is giving me a syntax error on the FROM clause.

    SELECT [Material Cost Table 1].[SOURCE], [Material Cost Table 1].[ITEM_NO], [Material Cost Table 1].[COST], [Material Cost Table 1].[DATE]
    FROM [Material Cost Table 1] INNER JOIN [Select Max([Material Cost Table 1].[DATE]) AS MaxDate, [Material Cost Table 1].[COST]
    FROM [Material Cost Table 1]
    GROUP BY [Material Cost Table 1].[COST]] AS qryMaxDate ON (Material Cost Table 1.COST = qryMaxDate.COST) AND (Material Cost Table 1.DATE = qryMaxDate.DATE);

  6. #6
    live2ride is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jun 2012
    Posts
    70
    you have no join

    select (fields)
    from table1
    inner join (select max(date), cost from table1) as table2
    on table1.id = table2.id
    group by (fields)

  7. #7
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    I put the whole SQL from the first query into the inner join section. Now my problem is that it is asking me for values for qryMaxDate.COST and .DATE. The inner join is in red. I think I am following the format Live2ride is suggesting.

    SELECT [Material Cost Table 1].[SOURCE], [Material Cost Table 1].[ITEM_NO], [Material Cost Table 1].[COST], [Material Cost Table 1].[DATE]
    FROM [Material Cost Table 1]
    INNER JOIN (SELECT [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO, Max([Material Cost Table 1].TRANS_DATE) AS MaxOfDATE
    FROM [Material Cost Table 1]
    GROUP BY [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO)
    AS qryMaxDate
    ON ([Material Cost Table 1].COST = qryMaxDate.COST) AND ([Material Cost Table 1].DATE = qryMaxDate.DATE);

  8. #8
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    I apologize. I am new to posting and have just learned how annoying it is when questions are cross posted and there is no link to the other post. This question has also been posted on MrExcel at http://www.mrexcel.com/forum/showthr...ent-cost-query.

  9. #9
    live2ride is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jun 2012
    Posts
    70
    you do not have fields cost and date in select statement for qryMaxDate. your qryMaxDate has fields (Source,Item_No,MaxOfDate)
    you are joining on fields which do not exist.

  10. #10
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    Thank you. I didn't realize that. It works perfectly now.

    SELECT [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO, [Material Cost Table 1].COST, [Material Cost Table 1].DATE
    FROM [Material Cost Table 1]
    INNER JOIN (SELECT [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO, Max([Material Cost Table 1].DATE) AS MaxOfDATE
    FROM [Material Cost Table 1]
    GROUP BY [Material Cost Table 1].SOURCE, [Material Cost Table 1].ITEM_NO) AS qryMaxDate
    ON ([Material Cost Table 1].SOURCE = qryMaxDate.SOURCE)
    AND ([Material Cost Table 1].ITEM_NO = qryMaxDate.ITEM_NO)
    AND ([Material Cost Table 1].DATE = qryMaxDate.MaxOfDATE);

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with most recent 2 weeks query.
    By tplee in forum Queries
    Replies: 7
    Last Post: 11-30-2011, 08:05 PM
  2. Most recent data in query
    By LisaEllen in forum Queries
    Replies: 4
    Last Post: 09-14-2011, 09:20 PM
  3. Join issues on packaing cost query.
    By Saniphor in forum Queries
    Replies: 0
    Last Post: 04-28-2011, 07:13 AM
  4. Replies: 4
    Last Post: 05-04-2010, 03:33 PM
  5. Cost of a database
    By P5C768 in forum Access
    Replies: 1
    Last Post: 04-22-2010, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums