I need to make a select query of employees and add two columns that consists of a count of how many items they have from another table.
I tried to get it to work with joins but couldn't get it to work. I finally got it to work with subqueries, but would like to know if there is a way to do it with Joins.
This is the code I got to work:
Select e.Id, e.Name, e.Description,
(select COUNT(*) from Equipment WHERE EmployeeId = e.Id and TypeId = 1) as 'SmartphoneCount',
(select COUNT(*) from Equipment WHERE EmployeeId = e.Id and TypeId = 2) as 'labtopCount'
FROM Employees as e
Which results in the following output:
Is there a better way to get the same output?
