我知道这里有很多问题,我试图将其他例子合并到我自己的例子中,但我似乎无法做到这一点。
我有sku,日期和费用的列,我想查看所有3列,但只能按最大日期,按sku分组。 目前:
Sku Date Cost
1 06/24/15 .01
1 02/22/14 .02
2 06/24/15 .04
2 02/22/14 .05
需要:
Sku Date Cost
1 06/24/15 .01
2 06/24/15 .04
这就是我的SQL:
SELECT dbo_SKU.PROD_CODE AS Sku, dbo_LOTS.REC_DATE AS [Last Date],
dbo_LOT_ITEM.COST AS Cost
FROM (dbo_LOTS INNER JOIN dbo_SKU ON dbo_LOTS.SKU_ID = dbo_SKU.SKU_ID)
INNER JOIN dbo_LOT_ITEM ON dbo_LOTS.LOT_ID = dbo_LOT_ITEM.LOT_ID;
以下是设计视图的样子(我更像是一个视觉人物): Design View
这是教我自己如何操作Access以及它如何运作的第2周,所以如果我们能用蜡笔打破这个问题,那将是非常好的。
答案 0 :(得分:2)
您可以添加其他逻辑来获取上次日期。一种方法是在WHERE
子句中添加相关子查询:
SELECT s.PROD_CODE AS Sku, l.REC_DATE AS [Last Date], li.COST AS Cost
FROM (dbo_LOTS as l INNER JOIN
dbo_SKU as si
ON l.SKU_ID = s.SKU_ID
) INNER JOIN
dbo_LOT_ITEM as li
ON l.LOT_ID = li.LOT_ID
WHERE l.REC_DATE = (SELECT MAX(l2.REC_DATE)
FROM dbo_LOTS as l2
WHERE l2.SKU_ID = l.SKU_ID
);
答案 1 :(得分:0)
这有效:
SELECT dbo_SKU.PROD_CODE AS Sku, dbo_LOTS.REC_DATE AS [Last Date], dbo_LOTS.COSTPERSKU AS Cost
FROM dbo_LOTS INNER JOIN dbo_SKU ON dbo_LOTS.SKU_ID = dbo_SKU.SKU_ID
WHERE (((dbo_LOTS.REC_DATE)=(SELECT MAX(l2.REC_DATE)
FROM dbo_LOTS as l2 WHERE l2.SKU_ID = dbo_LOTS.SKU_ID)));