MS ACCESS:无法根据最大日期检索最终价格

时间:2016-10-17 16:42:54

标签: sql ms-access

目的

根据COMPANY ID和最新ITEM ID付费(基于PRICE)制定LAST SHIP DATE的销售目录。

APPROACH

  1. 提入CUSTOMERSALESITEM表格
  2. 根据CUSTOMER IDITEM运行查询链接表以了解购买历史记录
  3. 导出显示COMPANY IDITEM IDLAST SALES PRICELAST SHIP DATE
  4. 的表格

    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 /问题

    1. CUSTOMER A 在3个不同的日期购买了ITEM ABC @ 3个不同的价格。我已采用Max Ship Date希望显示最后 PRICE PAID (产生一个单一的价格值) )。但是,出于某种原因,我仍然在三个不同的日期收到三种不同的价格如何让MS Access仅显示基于最新发货日期的最新价格?
    2. 注意:SDLPRC =“售价”。我必须将SLDPRC转换为货币然后除以1000;这是由于我们当前的数据库设置。此外,SDAITM是一个更加客户友好的“缩写项目编号”。

1 个答案:

答案 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]