2017-08-15 98 views
-1

請原諒我的格式...我正在努力。選擇正確的日期

我想在我的查詢中根據適當的生效日期選擇正確的項目價格。

例如,我們有一個具有以下信息的表格

價格表-------------------------

項目價格EFFECTIVEDATE

  • 一個$ 0.57 17年1月1日
  • 一個$ 0.72 17年6月1日

現在我有一個生產表其中包含當天生產的產品,並將列出其生產日期的數量和生產。

生產表-------------------

項目數量productionDate

  • A 100 17年2月1日
  • A 100 7/1/17

現在,當我查詢這些我希望能夠選擇適當的價格給出了productionDate和effectiveDate。

達到此目的的最佳方法是什麼?

回答

2

試試下面的(第一選擇最高EFFECTIVEDATE比productionDate下,然後得到該日的價格):你是很新的查詢和SQL,內部查詢

SELECT preselection.Item, Price FROM 
(SELECT Production.Item, Max(effectiveDate) As MaxEffectiveDate 
FROM Production INNER JOIN Price ON Price.item = Production.Item 
Where Price.effectiveDate <= productionDate GROUP BY Production.Item) As preselection 
INNER JOIN Price ON Price.Item = preselection.Item 
AND Price.effectiveDate = preselection.MaxEffectiveDate 
+1

@詹姆斯在情況(在括號中)可以保存爲一個單獨的查詢,例如[Get Production Effective Date]。然後,可以在另一個查詢中引用保存的查詢,例如'SELECT preselection.Item,Price FROM [Get Production Effective Date]作爲預選INNER JOIN Price ON Price.Item = preselection.Item AND Price.effectiveDate = preselection.MaxEffectiveDate'。這種方法的好處是可以使用Access Design View單獨編輯和測試這兩個查詢。 –

相關問題