Skip to content
Advertisement

SELECT COUNT from two tables is not working

I have two tables – first table is “projects”:

p_id p_name
1 test1
2 test2
3 test2
4 test3

p_name is not unique

The second table is “employee”:

e_id jan feb mar apr may
1 2 2 3 3 4
2 1 1 3 3 2
3 1 3 2 3 3
4 4 3 2 3 4

I’d like to count how many p_name in employee depending on name

What I do:

But it doesn’t work. It doesn’t return the expected numbers.

Advertisement

Answer

I completely agree with the comment of @sTTu – the chosen design is not scalable and will run into problems as soon as a “new year” starts.

Purely out of curiousity I tried to come up with a solution nonetheless which would be the following:

(SQL-Server – but should also work in MySql)

Here is a working snippet: https://rextester.com/TBWZA82894

Result:

name Jan feb mar
test1 2 1 0
test2 1 3 4
test3 1 0 0

Or, in an even simpler way you can do it like this:

see the demo here: https://rextester.com/DEUUE59264

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement