2013-02-14 101 views
0

有誰能告訴我爲什麼這個簡單的JOIN查詢爲我提供了一個沒有結果的表?當我分別運行countINTmonth和countDEPTmonth查詢時,他們給我我期望的結果。但是,當我運行簡單的JOIN查詢時,它給了我一個包含列名但沒有數據的表 - 但也沒有任何錯誤消息!MS Access SQL - 連接兩個其他查詢的簡單查詢沒有結果

CountINTmonth查詢

SELECT Count(clients.ssn) AS CountofIntakes, month(clients.prog_start) AS Month2, year(clients.prog_start) AS Year2 
FROM clients 
WHERE clients.prog_start BETWEEN [Enter Start Date] AND [Enter End Date] 
GROUP BY year(clients.prog_start), month(clients.prog_start) 
ORDER BY year(clients.prog_start) DESC , month(clients.prog_start) DESC; 

CountDEPTmonth查詢

SELECT Count(clients.ssn) AS CountOfDepartures, month(clients.[departure date]) AS Month1, year(clients.[departure date]) AS year1 
FROM clients 
WHERE clients.[departure date] BETWEEN [Enter Start Date] AND [Enter End Date] 
GROUP BY year(clients.[departure date]), month(clients.[departure date]) 
ORDER BY year(clients.[departure date]) DESC , month(clients.[departure date]) DESC; 

是加入了前兩個

SELECT countdeptmonth.countofdepartures, countintmonth.countofintakes, countdeptmonth.month1, countdeptmonth.year1 
FROM countdeptmonth 
INNER JOIN 
countintmonth 
ON (countdeptmonth.year1=countintmonth.year2 AND countdeptmonth.month1=countintmonth.month2) 
ORDER BY countdeptmonth.year1 DESC, countdeptmonth.month1 DESC 

當我改變連接到LEFT JOIN的查詢,會出現不同的是數據countofintakes列是空的。當我將JOIN更改爲RIGHT JOIN時,唯一有數據的列是記錄次數。

關於表格的注意事項
CLIENTS表格有一堆帶有不同數據點的列。 SSN是主要關鍵。 prog_start是他們開始治療計劃時的日期字段。 [出發日期]是他們離開我們的計劃時的日期字段。 [Enter Start Date]和[Enter End Date]應該是用戶在運行查詢時輸入的日期範圍 - 兩個查詢的日期應該相同。任何幫助將不勝感激!

+0

試着在沒有年份的月份加入,看看你是否得到匹配。你確定有記錄匹配年份和月份嗎? – AaronLS 2013-02-14 17:35:54

+0

我剛剛嘗試加入正月,仍然沒有數據結果。也許我對年/月記錄匹配感到困惑。當我運行每個單獨的查詢時,對於相同的日期範圍,每個日期範圍的每個月份都有一行。所以,在我看來,他們應該能夠在每個月和每年匹配時加入。 – tmolloy8 2013-02-14 17:45:26

回答

1

我可能已經解決了我自己的問題。我只是將MonthName函數添加到Month1和Month2列。

MonthName(month(clients.prog_start)) AS Month2 

而且

MonthName(month(clients.[departure date])) AS Month1 

一旦我做到了,連接查詢顯示正確的結果。我甚至不需要更新GROUP BY字段。

任何人都知道爲什麼查詢無法加入月份的數字以及爲什麼需要名稱才能正確加入?