2015-02-10 29 views
0

我想從table1的techID做外部連接從表2拉出日期。問題是techID在每個表中重複多次。我希望按順序列出日期,但一旦沒有更多數據就停止匹配。例如匹配沒有重複條目 - MS訪問

techID |Order# 
tech1 order1 
tech2 order2 
tech3 order3 
tech4 order4 
tech3 order5 
tech3 order6 

techID |Month 
tech3 Oct-01 
tech3 Nov-02 
tech2 Jan-10 
tech1 Jan-11 
tech1 Feb-02 
tech4 Feb-04 

output 
tech1 Feb-02 
tech2 Jan-10 
tech3 Oct-01 
tech4 Feb-04 
tech3 Nov-02 
tech3 

現在我所有的只是一個子查詢,但它遠沒有接近我所需要的...幫助!


(select last(table2.[shipping_date]) 
from table2 
where table1.techid = table2.techid) AS shippedon 

回答

0

你需要的是INNER JOIN與月份上的MAX字段和科技ID上的GROUP BY。喜歡的東西

SELECT 
    techTableName.techID, 
    Max(projectTableName.monthFieldName) As MaxOfMonth 
FROM 
    techTableName 
    INNER JOIN 
    projectTableName 
    ON 
    techTableName.techID = projectTableName.techID 
GROUP BY 
    techTableName.techID; 

不過,我看不出任何一點JOIN荷蘭國際集團他們,這個假設是根據您所提供的數據集,查詢可能是簡單的。

SELECT 
    projectTableName.techID, 
    Max(projectTableName.monthFieldName) As MaxOfMonth 
FROM 
    projectTableName 
GROUP BY 
    projectTableName.techID 
+0

我想查看所有發貨日期,而不僅僅是最後一個。這會工作嗎?技術名稱出現三次,並有兩個發貨日期。我希望看到這兩個發貨日期以及一個技術人員沒有發貨日期的事實。 – 2015-02-12 15:26:47

+0

請編輯您的原始文章並輸入您希望查看的正確數據和輸出。 – PaulFrancis 2015-02-12 15:43:53