I'm working my way through this explanation of Common Table Expressions at MSDN and I came across the code reproduced below. Can anyone explain to me what it does and how it does it?
I'm pretty sure I understand the concept of the view that is created in the first half. But the SELECT that joins on that view twice is confusing me.
CREATE VIEW vwMyView AS
SELECT EmployeeID, COUNT(*) AS NumOrders, MAX(OrderDate) AS MaxDate
FROM Orders
GROUP BY EmployeeID
SELECT
e.EmployeeID, oe.NumOrders, oe.MaxDate, e.ReportsTo AS ManagerID,
om.NumOrders, om.MaxDate
FROM
Employees AS e
INNER JOIN vwMyView AS oe ON e.EmployeeID = oe.EmployeeID
INNER JOIN vwMyView AS om ON e.ReportsTo = om.EmployeeID