1

I have a table with three columns: Item, Quantity, and Date.

The values in the Item column may be duplicates, but the Quantity and Dates will be unique.

For example:

Item - Quantity - Date
Hammer - 3 - 1/12/15
Hammer - 7 - 5/18/15
Hammer - 6 - 8/1/15
Wrench - 8 - 2/24/15
Wrench - 3 - 6/10/15

I am trying to write a query that will only return:

Item - Quantity - Date
Hammer - 6 - 8/1/15
Wrench - 3 - 6/10/15

This is my code:

SELECT DISTINCT stock.stc_st AS Store, stock.art_st AS UPC, articles.descr AS Description, stock.quan_st AS Quantity, articles.rp AS Cost
FROM stock LEFT JOIN articles ON stock.art_st = articles.article
WHERE stock.ym_st = 
(SELECT Max(stock.ym_st) 
 FROM stock t1
 WHERE stock.art_st=t1.art_st 
 GROUP BY t1.art_st)

GROUP BY stock.stc_st, stock.art_st, articles.descr, stock.quan_st, articles.rp, articles.act, articles.stat
HAVING (((stock.stc_st)=[Which Store?]) AND ((articles.act)="Y") AND ((articles.stat)="Y"));

However, my code is returning all items when I only want it to return the items with the max date. If anyone could take a look at this and tell me what I am doing wrong, I would really appreciate it.

========================

Now I'm trying to use this code from the answers below and it's giving me a Syntax Error on JOIN on the Inner Join at tmaxdate.art_st. I'm sure this is something stupid like a parenthesis out of place. Could anyone more familiar with Access's SQL syntax tell me what I'm doing wrong? Thanks!

    SELECT DISTINCT stock.stc_st AS Store, stock.art_st AS UPC, articles.descr AS Description, stock.quan_st AS Quantity, articles.rp AS Cost
FROM stock AS t1

INNER JOIN
    (
        SELECT tmaxdate.art_st, Max(tmaxdate.ym_st) AS MaxOfDate
        FROM stock AS tmaxdate
        GROUP BY tmaxdate.art_sc
    ) AS sub
    ON (t1.ym_st = sub.MaxOfDate) AND (tmaxdate.art_st = sub.art_st)


LEFT JOIN articles ON stock.art_st = articles.article
GROUP BY stock.stc_st, stock.art_st, articles.descr, stock.quan_st, articles.rp, articles.act, articles.stat
HAVING (((stock.stc_st)=[Which Store?]) AND ((articles.act)="Y") AND ((articles.stat)="Y"));
  • An Access query with more than one join requires parentheses in the `FROM` clause ... separate from those which enclose the subquery. See [Multiple INNER JOIN SQL ACCESS](http://stackoverflow.com/questions/20929332/multiple-inner-join-sql-access) for an example. – HansUp Aug 13 '15 at 13:39
  • I'm sorry to be a pain and I really appreciate your help. Would you look at the code I put in my revision to my original question and tell me where I need to put parenthesis. I looked at your other article and it seems pretty cut and dry there, but I've tried 5 or 6 different combos of parens and everything is kicking me back a syntax error. – Lance Charger Aug 13 '15 at 13:49
  • Thank you so much, by the way. – Lance Charger Aug 13 '15 at 13:49
  • Start with a simpler query which focuses on the join problem: `SELECT * FROM (stock AS t1 INNER JOIN ( SELECT tmaxdate.art_st, Max(tmaxdate.ym_st) AS MaxOfDate FROM stock AS tmaxdate GROUP BY tmaxdate.art_sc) AS sub ON (t1.ym_st = sub.MaxOfDate) AND (tmaxdate.Item = sub.art_st)) LEFT JOIN articles ON stock.art_st = articles.article` If Access accepts that, you can then choose the output fields, and add back the `GROUP BY` and `HAVING` clauses. – HansUp Aug 13 '15 at 13:56

2 Answers2

0

This has not been tested, but it is more along the lines of what you need. It uses a subquery to find the maximum date for each primary key of stock table (I have assumed this is art_st )

SELECT stock.stc_st AS Store
     , stock.art_st AS UPC
     , articles.descr AS Description
     , stock.quan_st AS Quantity
     , articles.rp AS Cost
FROM ( stock 
       LEFT JOIN articles 
              ON stock.art_st = articles.article
     )
     INNER JOIN (SELECT t1.art_st,  Max(stock.ym_st)  AS t1MaxDate
                   FROM stock t1
                 GROUP BY t1.art_st
                ) AS TabMax
               ON ( TabMax.art_st = stock.art_st
                  AND TabMax.t1MaxDate = stock.ym_st )
GROUP BY stock.stc_st
    , stock.art_st
    , articles.descr
    , stock.quan_st
    , articles.rp
    , articles.act
    , articles.stat
HAVING (((stock.stc_st)=[Which Store?]) AND ((articles.act)="Y") AND ((articles.stat)="Y"));
HarveyFrench
  • 4,440
  • 4
  • 20
  • 36
  • Thanks for your responses guys! I get that I need to do a subquery to GROUP BY the max date, but whatever I try keeps kicking me back syntax errors. I tried using your query Harvey and Access gave me a syntax error on the FROM clause. I know I can make this work and it's something stupid like a parenthesis in the wrong place, I just can't figure it out. – Lance Charger Aug 13 '15 at 13:13
  • Sorry try replacing JOIN with INNER JOIN – HarveyFrench Aug 13 '15 at 13:38
0

I couldn't figure out how that sample data is distributed among your tables. So I stored those data in a table named YourTable.

First create a GROUP BY query to show you the most recent Date for each Item:

SELECT t1.Item, Max(t1.Date) AS MaxOfDate
FROM YourTable AS t1
GROUP BY t1.Item

Then you can use that as a subquery which you join back to the main table in order to select only its rows with the matching Item/Date pairs:

SELECT t2.Item, t2.Quantity, t2.Date
FROM
    YourTable AS t2
    INNER JOIN
    (
        SELECT t1.Item, Max(t1.Date) AS MaxOfDate
        FROM YourTable AS t1
        GROUP BY t1.Item
    ) AS sub
    ON (t2.Date = sub.MaxOfDate) AND (t2.Item = sub.Item);

With your sample data in Access 2010, that query returns your requested output.

Since you don't actually have a single YourTable, you will need to adapt that approach for your actual tables, but this strategy should work there, too.

HansUp
  • 95,961
  • 11
  • 77
  • 135
  • SELECT DISTINCT stock.stc_st AS Store, stock.art_st AS UPC, articles.descr AS Description, stock.quan_st AS Quantity, articles.rp AS Cost FROM stock AS t1 INNER JOIN ( SELECT tmaxdate.art_st, Max(tmaxdate.ym_st) AS MaxOfDate FROM stock AS tmaxdate GROUP BY tmaxdate.art_sc ) AS sub ON (t1.ym_st = sub.MaxOfDate) AND (tmaxdate.Item = sub.art_st); – Lance Charger Aug 13 '15 at 13:24
  • LEFT JOIN articles ON stock.art_st = articles.article GROUP BY stock.stc_st, stock.art_st, articles.descr, stock.quan_st, articles.rp, articles.act, articles.stat HAVING (((stock.stc_st)=[Which Store?]) AND ((articles.act)="Y") AND ((articles.stat)="Y")); – Lance Charger Aug 13 '15 at 13:24
  • This is giving me a syntax error. What am I doing wrong? There is a clearer look at the code in the edit of my original question. – Lance Charger Aug 13 '15 at 13:24
  • Discard the semi-colon here: `AND (tmaxdate.Item = sub.art_st);` What happens after you remove it? – HansUp Aug 13 '15 at 13:29
  • Syntax error (missing operator) in query expression '(t1.ym_st=sub.MaxOfDate) AND (tmaxdate.art_st=sub.art_st) LEFT JOIN articles ON stock.art_st = articles.articl'. – Lance Charger Aug 13 '15 at 14:56
  • OK. I think the remaining problem may be the parentheses issue. Have you tried the simple query example I posted in a comment under the question? – HansUp Aug 13 '15 at 14:58