SQL in MS-Access: Using COUNT, JOIN and returning 0s

余生颓废 提交于 2020-01-06 06:08:54

问题


Apologies for posting this but although there are a few examples on the site, I just can't get mine to work.

So I have two tables as follows:

A Telephony table

ID | Name | GradeID
1    Richard   1
2    Allan     1
3    Peter

I also have a Grade table:

ID | Name
1    1
2    2
3    3
4    4
5    5

Anyway I'm trying to use COUNT() and LEFT JOIN to find out the number of times each grade is found in the Telephony table, including returning any which are 0, by using the following query:

SELECT telephony.GradeID, COUNT(*) AS Total
FROM telephony LEFT JOIN grade 
ON telephony.GradeID = grade.ID
GROUP BY telephony.GradeID
ORDER BY 1;

This query returns all found but will not return all grades with 0 entries:

Grade | Total
1       2

Please help. I'm using Microsoft Access 2003.


Thanks for all your help. That's working great.

However, when I try to incorporate a DATE BETWEEN it returns only the grades found again.

Any ideas?

Thanks


回答1:


I think this is what you are looking for:

SELECT grade.ID, Count(telephony.ID) AS CountOfID
FROM grade LEFT JOIN telephony ON grade.ID= telephony.GradeID
GROUP BY grade.ID
ORDER BY 1;

BTW: "Name" is a very bad name for a column since it is a reserved word.




回答2:


As only Grade table contains all available IDs you should use grade.ID in your SELECT instead of telephony.GradeID. Try to execute this query:

SELECT grade.ID, COUNT(telephony.ID) FROM grade
LEFT JOIN telephony ON telephony.GradeID = grade.ID
GROUP BY grade.ID
ORDER BY 1;


来源:https://stackoverflow.com/questions/3406610/sql-in-ms-access-using-count-join-and-returning-0s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!