2017-06-06 98 views
1

我在写一个查询,并且无法按照我的意愿过滤数据。在表中,有一个日期字段和一个ItemCode字段。我希望每个ItemCode返回一个记录,其中最早的日期是今天之后。今天日期之后的SQL最小日期值现在()

如果今天是2017年6月6日,我的数据是这样的:

ItemCode  Date 
1   6/1/2017 
1   6/7/2017 
1   6/10/2017 
2   6/2/2017 
2   6/8/2017 
2   6/15/2017 

我希望得到的结果是

ItemCode  Date 
1   6/7/2017 
2   6/8/2017 

我的查询到目前为止是:

SELECT PO_PurchaseOrderDetail.ItemCode, Min(PO_PurchaseOrderDetail.RequiredDate) AS NextPO 
FROM PO_PurchaseOrderDetail 
GROUP BY PO_PurchaseOrderDetail.ItemCode 
HAVING (((Min(PO_PurchaseOrderDetail.RequiredDate))>=Now())); 

问题在于Min函数先触发并抓取每个ItemCode的最早日期,这是今天之前的日期。然后计算> = Now(),并且由于最短日期在今天之前,所以查询不会返回任何内容。

我试过在查询的HAVING部分的min函数中放入> = Now(),但它不会改变结果。

我的结构是错误的,我将不胜感激任何建议。谢谢!

回答

1

我将接近这样的标准SQL,访问方式可能会有所不同

select PO_PurchaseOrderDetail.ItemCode, 
     min(PO_PurchaseOrderDetail.RequiredDate) as NextPO 

from PO_PurchaseOrderDetail 
where PO_PurchaseOrderDetail.RequiredDate >= Now() 
group by PO_PurchaseOrderDetail.ItemCode; 
+0

完美。此外,它在Access中工作得很好。谢谢! –

1

把日期条件的where条款(不having条款)中:

select ItemCode, min(Date) as NextPO 
from PO_PurchaseOrderDetail 
where Date > '6/6/2017' 
group by ItemCode 
+0

这个和JohnHC的答案都很好。谢谢! –