目的
根据COMPANY ID
和最新ITEM ID
付费(基于PRICE
)制定LAST SHIP DATE
的销售目录。
APPROACH
CUSTOMER
,SALES
,ITEM
表格CUSTOMER ID
和ITEM
运行查询链接表以了解购买历史记录COMPANY ID
,ITEM ID
,LAST SALES PRICE
,LAST SHIP DATE
CODE
SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
(CCur([SDLPRC])/10000) AS PRICE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM,
(CCur([SDLPRC])/10000);
ISSUE /问题
CUSTOMER
A 在3个不同的日期购买了ITEM
ABC @ 3个不同的价格。我已采用Max
Ship Date
希望显示最后 PRICE
PAID (产生一个单一的价格值) )。但是,出于某种原因,我仍然在三个不同的日期收到三种不同的价格。 如何让MS Access仅显示基于最新发货日期的最新价格? 注意:SDLPRC =“售价”。我必须将SLDPRC转换为货币然后除以1000;这是由于我们当前的数据库设置。此外,SDAITM
是一个更加客户友好的“缩写项目编号”。
答案 0 :(得分:2)
问题在于您按价格变量(CCur([SDLPRC])/10000)
进行分组。当您使用GROUP BY
时,Access / SQL将通过所有 GROUP BY
语句中的变量拆分行。所以你不需要按价格分组。
更改您的查询以使用子查询来查找按[Company ID]
,[Item ID]
和Style
分组的销售的最后日期。使用外部查询来获取该特定记录的价格。类似的东西:
SELECT b.[COMPANY ID], b.[ITEM ID], b.STYLE, b.[LAST SHIP DATE], CCur(a.[SDLPRC])/10000 as PRICE
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)] as a
INNER JOIN
(SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM
) as b
ON a.SDAN8 = b.[COMPANY ID]
and a.SDITM = b.[ITEM ID]
and a.SDAITM = b.STYLE
and a.SDDRQJ = b.[LAST SHIP DATE]