2015-04-02 61 views
0

我有四個表:加入兩個查詢在Access

客戶

ID | CLIENT_NUM | Year 

農場

ID | ClientID | FARM_NUM 

ID | FarmID | FIELD_NUM | RANK 

SoilSheet

ID | FarmID | FieldID | SAMP_NUM | Year 

我需要從Rank,FIELD_NUM和Year排序的Soilsheet中獲取數據。所以,我試過這個:

SELECT FL.Rank,FL.FIELD_NUM, S.Year, SAMP_NUM 
FROM (((SoilSheet S 
INNER JOIN Farms F ON F.ID = S.FarmID) 
INNER JOIN Clients C ON C.ID = F.ClientID) 
INNER JOIN Fields FL ON FL.ID = S.FieldID) 
WHERE C.CLIENT_NUM = 1 AND F.FARM_NUM = 1 
ORDER BY C.CLIENT_NUM, F.FARM_NUM, FL.Rank, FL.FIELD_NUM, S.Year 

問題是RANK可能會在每年不同,所以它沒有正確排序。例如,我需要按2015年的RANK排序。

通過上述,我得到這樣的結果:

RANK | FIELD_NUM | Year | SAMP_NUM 
    0 |  19-2 | 2015 | 3000 
    2 |  20-1 | 2015 | 3001 
    2 |  20-1 | 2014 | 2002 
    2 |  20-1 | 2015 | 1003 
    20 |  19-2 | 2014 | 2001 

19-2是RANK 0在2015年,但20在2014年因此,我嘗試這樣做:

SELECT FL.Rank,FL.FIELD_NUM, S.Year, SAMP_NUM 
FROM ((Clients C 
INNER JOIN Farms F ON F.ClientID = C.ID) 
INNER JOIN Fields FL ON FL.FarmID = F.ID) 
LEFT JOIN (((SoilSheet S 
INNER JOIN Fields FLS ON FLS.ID = S.FieldID) 
INNER JOIN Farms FS ON FS.ID = S.FarmID) 
INNER JOIN Clients CS ON CS.ID = FS.ClientID) 
ON 
(FLS.FIELD_NUM = FL.FIELD_NUM 
AND FS.FARM_NUM = F.FARM_NUM 
AND CS.CLIENT_NUM = C.CLIENT_NUM) 
WHERE C.CLIENT_NUM = 1 AND F.FARM_NUM = 1 AND C.Year = 2015 
ORDER BY C.CLIENT_NUM, F.FARM_NUM, FL.Rank, FL.FIELD_NUM, S.Year 

這工作正常SQL Server:

RANK | FIELD_NUM | Year | SAMP_NUM 
    0 |  19-2 | 2015 | 3000 
    0 |  19-2 | 2014 | 2001 
    2 |  20-1 | 2015 | 3001 
    2 |  20-1 | 2014 | 2002 
    2 |  20-1 | 2015 | 1003 

在Access中,它給出'Join expression not supported。'。我猜這是因爲Access不喜歡加入INNER連接,但我只是猜測。

任何想法?訂購或另一種方式來加入另一種方式?

+0

我建議你閱讀本:https://msdn.microsoft.com/ en-us/library/bb243855%28v = office.12%29.aspx – 2015-04-02 21:04:42

+0

從那篇文章中,「OUTER JOINs可以嵌套在多表連接中的INNER JOIN中,但INNER JOINs不能嵌套在OUTER JOIN中。」暗示我在左連接中不能有任何內連接,所以現在呢? – 2015-04-02 22:29:05

+0

我建議你通過表格連接表來找出問題何時開始。 MS Access數據庫引擎喜歡括號。在第一次查看時,查詢中括號的數量太小。 – 2015-04-03 05:39:45

回答

0

您是否試圖將TSQL從SQL Server複製到Access?不幸的是,如果不修改Joins,這將無法正常工作。具體來說,您需要在連接子句的周圍放置括號。

退房此瞭解更多信息:

SQL Inner Joins with multiple tables

+0

周圍有圓括號。這是它不喜歡的ON(FLS.FIELD_NUM = FL.FIELD_NUM AND FS.FARM_NUM = F.FARM_NUM AND CS.CLIENT_NUM = C.CLIENT_NUM)。 – 2015-04-02 22:27:17

+0

你在SQL視圖中工作嗎?使用Design View建立這些查詢時會發生什麼? (即你不需要考慮括號) – 2015-04-03 16:06:22

1

我終於得到它的工作

SELECT C1.Year,C1.RANK, C1.FIELD_NUM, C.CLIENT_NUM, F.FARM_NUM FROM 
(SELECT C.Year,FL.RANK, FL.FIELD_NUM, C.CLIENT_NUM, F.FARM_NUM FROM 
((Clients C 
INNER JOIN Farms F ON F.ClientID = C.ID) 
INNER JOIN Fields FL ON FL.FarmID = F.ID)) C1 
LEFT JOIN 
(SELECT S.Year, SAMP_NUM,FLS.FIELD_NUM, FS.FARM_NUM, CS.CLIENT_NUM 
FROM ((SoilSheet S 
INNER JOIN Fields FLS ON FLS.ID = S.FieldID) 
INNER JOIN Farms FS ON FS.ID = S.FarmID) 
INNER JOIN Clients CS ON CS.ID = FS.ClientID) S1 
ON (S1.FIELD_NUM = C1.FIELD_NUM AND S1.FARM_NUM = C1.FARM_NUM 
AND S1.CLIENT_NUM = C1.CLIENT_NUM) 
WHERE C.CLIENT_NUM = 1 AND F.FARM_NUM = 1 AND C.Year = 2015 
ORDER BY FL.Rank, FL.FIELD_NUM 
+0

最後你爲了使它起作用而改變了什麼?它是不是放置的parens? – 2015-04-14 17:58:16