Advertisement
shady_obeyd

14.Open/ClosedStatistics

Jun 21st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.77 KB | None | 0 0
  1. WITH CTE_OpenReports (Id, [Name], OpenReportsIds) AS (
  2. SELECT e.Id, e.FirstName + ' ' + e.LastName AS [Name],
  3.        COUNT(r.Id) AS OpenReportsIds
  4. FROM Employees AS e
  5. JOIN Reports AS r
  6. ON r.EmployeeId = e.Id
  7. WHERE YEAR(r.OpenDate) = '2016'
  8. AND r.CloseDate IS NULL
  9. GROUP BY e.Id, e.FirstName + ' ' + e.LastName)
  10.  
  11. SELECT COALESCE([or].[Name], cr.[Name]) AS [Name],
  12.        CONCAT(ISNULL(cr.ClosedReportsIds, 0), '/', ISNULL([or].OpenReportsIds, 0)) AS [Closed Open Reports]
  13. FROM (
  14.     SELECT e.Id, e.FirstName + ' ' + e.LastName AS [Name],
  15.            COUNT(r.Id) AS ClosedReportsIds
  16.     FROM Employees AS e
  17.     JOIN Reports AS r
  18.     ON r.EmployeeId = e.Id
  19.     WHERE YEAR(CloseDate) = '2016'
  20.     GROUP BY e.Id, e.FirstName + ' ' + e.LastName) AS cr
  21. FULL JOIN CTE_OpenReports AS [or]
  22. ON [or].Id = cr.Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement