I have a table called ProjectRessources with the following data:
+-----------+------------+---------+-----------+
| projectId | employeeId | nbHours | wageHours |
+-----------+------------+---------+-----------+
| 1 | 1876 | 500 | 65 |
| 1 | 4354 | 2000 | 31 |
| 2 | 2231 | 250 | 55 |
| 3 | 1212 | 3000 | 35 |
| 3 | 1876 | 2000 | 35 |
| 3 | 2231 | 500 | 65
I must query the employees that have worked on more than one projects to obtain something like this:
+------------+----------------+
| employeeId | nbProjects |
+------------+----------------+
| 1876 | 2 |
| 2231 | 2 |
+------------+----------------+
I have tried
SELECT DISTINCT employeeID,projectID
FROM ProjectRessources
WHERE COUNT(projectID) >1;
but I get an error 1111 on my use of COUNT
ERROR 1111 (HY000): Invalid use of group function
I have then attempted something like this
SELECT DISTINCT
employeeId ,
COUNT(projectId) as nbProjects
From ProjectRessources
WHERE nbProjects >1;
But then I obtain the following error
ERROR 1054 (42S22): Unknown column 'nbProjects' in 'where clause'
Please help, any feedback is appreciated. Please note that I am not allowed to use GROUP BY to make my query.
You can try joining the
ProjectResourcestable with itself as follows:This just tries to see for each row if there is another row that has the same
employeeIDvalue but a differentprojectIDvalue. We don't care if how many of them there are as long as there is at least one and that is why weselect distinctso that the sameemployeeIDdoes not appear more than once (without thedistinctkeyword, we would get one row for every project the employee worked on).I used the table from your updated question to create an actual table at sqlfiddle.com. Next time you can (and should) do this yourself and post the link in your question.
See Demo
But this sounds suspiciously like homework.
If you must have the counts:
View on DB Fiddle