Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH CTE_OpenReports (Id, [Name], OpenReportsIds) AS (
- SELECT e.Id, e.FirstName + ' ' + e.LastName AS [Name],
- COUNT(r.Id) AS OpenReportsIds
- FROM Employees AS e
- JOIN Reports AS r
- ON r.EmployeeId = e.Id
- WHERE YEAR(r.OpenDate) = '2016'
- AND r.CloseDate IS NULL
- GROUP BY e.Id, e.FirstName + ' ' + e.LastName)
- SELECT COALESCE([or].[Name], cr.[Name]) AS [Name],
- CONCAT(ISNULL(cr.ClosedReportsIds, 0), '/', ISNULL([or].OpenReportsIds, 0)) AS [Closed Open Reports]
- FROM (
- SELECT e.Id, e.FirstName + ' ' + e.LastName AS [Name],
- COUNT(r.Id) AS ClosedReportsIds
- FROM Employees AS e
- JOIN Reports AS r
- ON r.EmployeeId = e.Id
- WHERE YEAR(CloseDate) = '2016'
- GROUP BY e.Id, e.FirstName + ' ' + e.LastName) AS cr
- FULL JOIN CTE_OpenReports AS [or]
- ON [or].Id = cr.Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement