Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT e.FirstName + ' ' + e.LastName AS Name,
- CASE WHEN f.CountUsers IS NULL THEN 0 ELSE f.CountUsers END AS [Users Number]
- FROM Employees AS e
- LEFT JOIN (
- SELECT r.EmployeeId, COUNT(r.UserId) AS CountUsers
- FROM(
- SELECT EmployeeId, UserId
- FROM Reports
- WHERE EmployeeId IS NOT NULL
- GROUP BY EmployeeId, UserId
- ) as r
- GROUP BY r.EmployeeId
- ) AS f ON f.EmployeeId = e.Id
- ORDER BY f.CountUsers DESC, e.FirstName + ' ' + e.LastName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement