As in the comments, you need a GROUP BY
clause when you want to have aggregate results (like it seems you want: "Number of Studies per project, company and user"). So, the first thing to do is add:
GROUP BY project.projectId, company.companyName, user.userName
Notice that the three columns are exactly the three that you have (unaggregated) in the SELECT
list.
SELECT
project.projectId,
company.companyName,
user.userName,
COUNT(study.studyId) AS numberStudies
FROM project, company, user, study
WHERE company.companyId = project.projectCompanyId,
AND user.userId = project.projectUserId,
AND study.studyProjectId = project.projectId
GROUP BY project.projectId, company.companyName, user.userName
ORDER BY company.companyId, user.userId, project.projectDate ;
This will show what you want but there are still a few issues:
- First, you are using the old (SQL-89) syntax of implicit joins with the conditions in the
WHERE
clause. This syntax is not deprecated but the new (well, 20 years old SQL-92) syntax with theJOIN
keyword has several advantages. - We can add aliases for the tables for readability.
- There may be two companies or users with same name so we should group by their IDs, not only their names.
- One advantage of explicit
JOIN
syntax is that it's easy to have results when there are no rows to join (as you want to show when there is no studies for a project). JustLEFT JOIN
thestudy
table.
So, the query becomes:
SELECT
p.projectId,
c.companyName,
u.userName,
COUNT(s.studyId) AS numberStudies
FROM
project AS p
JOIN
company AS c ON c.companyId = p.projectCompanyId
JOIN
user AS u ON u.userId = p.projectUserId
LEFT JOIN
study AS s ON s.studyProjectId = p.projectId
GROUP BY
c.companyId,
u.userId,
p.projectId,
c.companyName, u.userName
ORDER BY
c.companyId,
u.userId,
p.projectDate ;