MS Access 最大日期和最近日期及其各自的数量

Posted

技术标签:

【中文标题】MS Access 最大日期和最近日期及其各自的数量【英文标题】:MS Access max date and recent date and their respective qty 【发布时间】:2015-10-01 02:46:56 【问题描述】:

寻找 MaxDate,它的最近日期和间隔。在 MaxDate 和最近的日期中,我还需要每个的数量,这样我也可以找到间隔

表“tblITEM_InventoryCount”结构如下:

Item_No 计数日期数量

001 08/29/2015 12

001 08/15/2015 17

001 07/15/2015 19

项目编号 001

Max(CountDate) 08/29/2015

PriorCountDate 08/15/2015

间隔天数(MaxDate-RecentDate)14

MaxDate 数量 12

PriorCountDate 数量 17

间隔数量(17-12)5

当前使用查询来查找每个 ITEM_NO 的最后两个计数日期

SELECT tblITEM_InventoryCount.ITEM_NO, tblITEM_InventoryCount.Quantity, tblITEM_InventoryCount.CountDate
FROM tblITEM_InventoryCount
WHERE (((tblITEM_InventoryCount.CountDate)>=NthInGroup([tblITEM_InventoryCount].[ITEM_NO],2)))
ORDER BY tblITEM_InventoryCount.ITEM_NO, tblITEM_InventoryCount.CountDate DESC;

然后我使用第二个查询来计算我的数据:

SELECT qryLAST2_InventoryCount_TRANSACTIONS.ITEM_NO,      qryLAST2_InventoryCount_TRANSACTIONS.CountDate, (SELECT MAX([CountDate]) FROM [qryLAST2_InventoryCount_TRANSACTIONS] AS [Old Orders] WHERE [Old Orders].[CountDate] < [qryLAST2_InventoryCount_TRANSACTIONS].[CountDate] AND [Old Orders].[ITEM_NO] = [qryLAST2_InventoryCount_TRANSACTIONS].[ITEM_NO]) AS PriorCountDate, [CountDate]-[PriorCountDate] AS DaysInterval, qryLAST2_InventoryCount_TRANSACTIONS.Quantity, (SELECT Last([Quantity]) FROM [qryLAST2_InventoryCount_TRANSACTIONS] AS [OldCount] WHERE [OldCount].[Quantity] < [qryLAST2_InventoryCount_TRANSACTIONS].[Quantity] AND [OldCount].[ITEM_NO] = [qryLAST2_InventoryCount_TRANSACTIONS].[ITEM_NO]) AS PriorQuantity, [Quantity]-[PriorQuantity] AS QuantityInterval, [QuantityInterval]*30/[DaysInterval] AS [Usage]
FROM qryLAST2_InventoryCount_TRANSACTIONS
GROUP BY qryLAST2_InventoryCount_TRANSACTIONS.ITEM_NO, qryLAST2_InventoryCount_TRANSACTIONS.CountDate, qryLAST2_InventoryCount_TRANSACTIONS.Quantity
ORDER BY qryLAST2_InventoryCount_TRANSACTIONS.ITEM_NO, qryLAST2_InventoryCount_TRANSACTIONS.CountDate DESC;

我没有得到我需要的结果。查询返回每个项目的两条记录行以及它们的最大或最后计数日期、上一个计数日期、间隔天数、数量、最后一个数量和间隔。

我需要 max 或 last countdate 及其数量计数和之前的 countdate 及其数量。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

试试这个:

 select 
   tab.item_no, 
   tab.Max_Date, 
   tab.PriorCountDate, 
   DateDiff ("d", tab.Max_Date, tab.PriorCountDate) as IntervalDays,
   tab.MaxDateQty,
   tab.PriorCountDateQty,
   ( tab.MaxDateQty-tab.PriorCountDateQty) as IntervalQty,


 from

   ( select 
          temp1.item_no as item_no, 
          temp1.m as Max_Date, 
          (select MAX(count_date) from tblITEM_InventoryCount where count_date <> temp1.m ) as PriorCountDate,
          temp1.q as MaxDateQty,
          (select MAX(Qty) from tblITEM_InventoryCount where Qty <> temp1.q) as PriorCountDateQty,


     from

       ( select item_no as i, Qty as q, MAX(count_date) as m
         group by item_no, Qty ) as temp1

       inner join

          tblITEM_InventoryCount as temp2   

       on

          temp1.item_no = temp2.item_no 
    )
     as tab  ;

【讨论】:

谢谢@Sagar,但在将这段代码粘贴到查询的 SQL 视图中后我仍然遇到困难。 @kduncan,你能说说会发生什么错误吗?

以上是关于MS Access 最大日期和最近日期及其各自的数量的主要内容,如果未能解决你的问题,请参考以下文章

MS Access 日期范围函数减慢查询速度

根据日期仅选择每条记录的最新版本 | MS Access [重复]

MS-Access:选择每个会计月的开始日期和日期

使用 Workbench 将 MS Access 迁移到 MySQL 时出现日期时间错误

MS Access:按开始日期和结束日期之间每个月的月份分组

获取 min(date) AND max(date) 及其各自的标题