View Full Version : Count number of fields using where


Xeon
10-12-2005, 09:25 PM
Hey,

I'm currently working on a database for my school project.

Currently, I have a table:

Job:
Contractor_ID [Foreign Key]
...

Contractors:
Contractor_ID [Primary Key]
Contractor [Text]

Basically I want to create a query that shows a table like so:
Wages:
Contractor [Contractors.Contractor]
Jobs_done [counts the number of rows from Job that has the Contractor_ID equal to the Contractor_ID specific to Contractor (above)]


Eg.


Job:
Contractor_ID
1
2
3
2
1
4

Contractors:
Contractor_ID Contractor
1 Bob
2 Bill
3 Jim
4 Jill

Wages:
Contractor Jobs_done
Bob 2
Bill 2
Jim 1
Jill 1

Jon K
10-12-2005, 10:16 PM
Type/Paste in the SQL View of a new query:-

SELECT Contractors.Contractor, Count(Job.Contractor_ID) AS Jobs_done
FROM Contractors INNER JOIN Job ON Contractors.Contractor_ID = Job.Contractor_ID
GROUP BY Contractors.Contractor;


If there are contractors who have no related records in the Job table and you want to include them in the query results, you can change INNER JOIN to LEFT JOIN in the SQL statement.
.