从另一个字段中选择MAX中来自类似字段的字段

时间:2015-08-12 17:29:26

标签: ms-access

我有一个包含三列的表:项目,数量和日期。

“项目”列中的值可能重复,但“数量”和“日期”将是唯一的。

例如:

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

我正在尝试编写一个只会返回的查询:

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

这是我的代码:

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"));

然而,当我只希望它返回具有最大日期的项目时,我的代码将返回所有项目。如果有人可以看看这个,并告诉我我做错了什么,我真的很感激。

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

现在我尝试使用下面的答案中的代码,并且它在tmaxdate.art_st上的内部联接上的JOIN上给出了语法错误。我确定这是一个像括号一样愚蠢的东西。任何更熟悉Access的SQL语法的人都可以告诉我我做错了什么吗?谢谢!

    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"));

2 个答案:

答案 0 :(得分:0)

这尚未经过测试,但它更符合您的需求。 它使用子查询来查找stock表的每个主键的最大日期(我假设这是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"));

答案 1 :(得分:0)

我无法弄清楚样本数据是如何在表中分配的。所以我将这些数据存储在名为 YourTable 的表中。

首先创建一个GROUP BY查询,向您显示每个项目的最新日期

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

然后,您可以将其用作连接回主表的子查询,以便仅选择具有匹配的 Item / Date 对的行:

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);

使用Access 2010中的示例数据,该查询将返回您请求的输出。

由于您实际上没有单个 YourTable ,因此您需要针对实际表格调整该方法,但此策略也应该适用于此。