daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jun 19th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT e.FirstName + ' ' + e.LastName AS Name,
  2.         CASE WHEN f.CountUsers IS NULL THEN 0 ELSE f.CountUsers END AS [Users Number]
  3. FROM Employees AS e
  4. LEFT JOIN (
  5.     SELECT r.EmployeeId, COUNT(r.UserId) AS CountUsers
  6.     FROM(
  7.         SELECT EmployeeId, UserId
  8.         FROM Reports
  9.         WHERE EmployeeId IS NOT NULL
  10.         GROUP BY EmployeeId, UserId
  11.     ) as r
  12.     GROUP BY r.EmployeeId
  13. ) AS f ON f.EmployeeId = e.Id
  14. ORDER BY f.CountUsers DESC, e.FirstName + ' ' + e.LastName
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top