0

谁能告诉我为什么这个简单的 JOIN 查询给了我一个没有结果的表?当我分别运行 countINTmonth 和 countDEPTmonth 查询时,它们会给出我期望的结果。但是,当我运行简单的 JOIN 查询时,它给了我一个包含列名但没有数据的表——但也没有任何错误消息!

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

当我将 JOIN 更改为 LEFT JOIN 时,会出现数据,但 countofintakes 列为空。当我将 JOIN 更改为 RIGHT JOIN 时,唯一具有数据的列是 countofintakes。

表上的注释
CLIENTS 表有一堆具有不同数据点的列。SSN 是主键。prog_start 是他们开始我们治疗计划的日期字段。[departure date] 是他们离开我们计划的日期字段。[输入开始日期] 和 [输入结束日期] 应该是用户在运行查询时输入的日期范围——两个查询的日期应该相同。任何帮助将不胜感激!

4

1 回答 1

1

I may have solved my own problem. I just added the MonthName function to the Month1 and Month2 columns.

MonthName(month(clients.prog_start)) AS Month2

And

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

Once I did that, the JOIN query showed the correct results. I didn't even need to update the GROUP BY fields.

Anyone know why the query couldn't join on the just the number of the month and why it needed the name to join correctly?

于 2013-02-14T17:57:21.513 回答