2013-07-12 31 views
2

我試圖運行一個查詢,以查找每個客戶和零件的最近PRICE。我的參數是This_Customer,This_Part和This_Date。任何留空的參數應該返回該字段的所有值。SQL在不影響查詢結果的情況下在查詢中選擇字段

TABLE1

ID | Customer | Part | BegDate | Price 
101 1   A  1/1/2013  $1 
102 2   A  2/1/2013  $2 
103 2   A  3/1/2013  $3 
104 1   B  4/1/2013  $4 
105 2   B  5/1/2013  $5 

通過運行以下兩個問題,我已經能夠做到發現每個客戶,部分,日期相應的記錄,但它只會工作,如果我排除價格。

第一個查詢使用DateDiff函數和< = 0條件來查找小於This_Date的所有日期。

查詢1「DaysSinceQuery」:

SELECT DateDiff("d",This_Date,BegDate) AS DaysSince, Table1.Part, Table1.Customer, Table1.BegDate, Table1.Price, Table1.ID 
FROM Table1 
WHERE (((DateDiff("d",This_Date,BegDate))<=0) AND ((Table1.Part) Like IIf(This_Part<>"",This_Part,"*")) AND ((Table1.Customer) Like IIf(This_Customer<>"",This_Customer,"*"))); 

第二查詢使用最大函數來找到與來自This_Date最少的天數記錄(最接近負DaysSince值或等於零)。

查詢2「NearestDateQuery」:

SELECT Max(DaysSinceQuery.DaysSince) AS NearestDate, Table1.Part, Table1.Customer 
FROM Table1 INNER JOIN DaysSinceQuery ON Table1.ID = DaysSinceQuery.ID 
GROUP BY Table1.Part, Table1.Customer; 

如何添加Table1.Price值NearestDateQuery而不影響NearestDateQuery結果?似乎應該有一個簡單的解決方案,但我無法得到它。

爲了進一步的清晰,下面是這個查詢應該如何工作的衆多例子。

謝謝你的幫助!

QUERY EXAMPLE 1 
This_Customer  2 
This_Part   A 
This_Date   2/15/2013 

ID | Customer | Part | BegDate | Price 
102 2   A  2/1/2013  $2 


QUERY EXAMPLE 2 
This_Customer  NULL 
This_Part   A 
This_Date   2/15/2013 

ID | Customer | Part | BegDate | Price 
102 2   A  2/1/2013  $2 


QUERY EXAMPLE 3 
This_Customer  2 
This_Part   NULL 
This_Date   5/15/2013 

ID | Customer | Part | BegDate | Price 
103 2   A  3/1/2013  $3 
105 2   B  5/1/2013  $5 


QUERY EXAMPLE 4 
This_Customer  2 
This_Part   A 
This_Date   NULL 

ID | Customer | Part | BegDate | Price 
102 2   A  2/1/2013  $2 
103 2   A  3/1/2013  $3 


QUERY EXAMPLE 5 
This_Customer  NULL 
This_Part   A 
This_Date   NULL 

ID | Customer | Part | BegDate | Price 
101 1   A  1/1/2013  $1 
102 2   A  2/1/2013  $2 
103 2   A  3/1/2013  $3 


QUERY EXAMPLE 6 
This_Customer  NULL 
This_Part   NULL 
This_Date   4/15/2013 

ID | Customer | Part | BegDate | Price 
103 2   A  3/1/2013  $3 
104 1   B  4/1/2013  $4 


QUERY EXAMPLE 7 
This_Customer  2 
This_Part   NULL 
This_Date   NULL 

ID | Customer | Part | BegDate | Price 
102 2   A  2/1/2013  $2 
103 2   A  3/1/2013  $3 
105 2   B  5/1/2013  $5 
+0

其中RDBMS是這樣嗎?查詢語法並不完全符合我所見過的任何內容,儘管與SQL服務器非常相似。 –

+0

Joachim,我在Access中這樣做。我檢查了我的帖子,發現一些拼寫錯誤不在我的原始SQL代碼中。這些錯誤現在得到糾正。對不起,錯誤。我的問題依然存在。 – likearock83

回答

0

我會用一個「指針」查詢,抓住最近的每個客戶和部分日期(在這種情況下,我只是用今天的日期使用日期()函數,但你可以使一個變量):

SELECT Table1.Customer, Table1.Part, Max(Table1.BegDate) AS MaxOfBegDate 
FROM Table1 
GROUP BY Table1.Customer, Table1.Part 
HAVING (((Max(Table1.BegDate))<=Date())); 

您可能必須將HAVING子句更改爲包含Part和Customer。

然後該指針加入到您的原始表中的ID和價格帶來的,適當的日期:

SELECT Table1.ID, Table1.Customer, Table1.Part, Table1.BegDate, Table1.Price 
FROM Query1 INNER JOIN Table1 ON (Query1.MaxOfBegDate = Table1.BegDate) AND (Query1.Part = Table1.Part) AND (Query1.Customer = Table1.Customer); 
+0

Johnny,謝謝!我在Part和Customer的第二個查詢中添加了一個WHERE子句,它完美地工作。事後看來,我在第一個查詢中使用WHERE而不是HAVING時出錯。如果有人在將來查看這篇文章想要了解更多有關WHERE vs HAVING的內容,請閱讀我在以下鏈接中找到的文章。 http://stackoverflow.com/a/9253267/2577090 – likearock83