Hi,
I am trying to find a way to write a query. I think it's easy, but I just don't know how to do it.
I have a table called "table1" looks like this:
EmployeeID CarID
-------------------
1 1
2 2
3 3
4 4
5 5
1 6
3 7
4 8
6 9
3 10
2 11
2 12
All I want is write a query to return the number of employee who has more than 3 cars.
The query I wrote is:
select count(Distinct EmployeeID) as num from table1 t where (select count(CarID) from table1 where t.EmployeeID = table1.EmployeeID) > 3
Is there another way to write query return the same result?
I am thinking about something like:
select count(EmployeeID) as num from table1 group by EmployeeID having (count(CarID)>3)
But it's not returning the correct value.
I am trying to find a way to write a query. I think it's easy, but I just don't know how to do it.
I have a table called "table1" looks like this:
EmployeeID CarID
-------------------
1 1
2 2
3 3
4 4
5 5
1 6
3 7
4 8
6 9
3 10
2 11
2 12
All I want is write a query to return the number of employee who has more than 3 cars.
The query I wrote is:
select count(Distinct EmployeeID) as num from table1 t where (select count(CarID) from table1 where t.EmployeeID = table1.EmployeeID) > 3
Is there another way to write query return the same result?
I am thinking about something like:
select count(EmployeeID) as num from table1 group by EmployeeID having (count(CarID)>3)
But it's not returning the correct value.