Microsoft SQL Server 2005 Best Practices

How to create an employee history view?

 

Execute the following script in Query Editor to create a view in the HumanResources schema:

USE AdventureWorks;
GO

CREATE VIEW [HumanResources].[vEmpDeptHistory]
AS
SELECT
Name = c.[LastName] + ', ' + c.[FirstName]
,Manager= cm.[LastName] + ', ' + cm.[FirstName]
,s.[Name] AS [Shift]
,d.[Name] AS [Department]
,d.[GroupName]
,StartDate=convert(varchar,edh.[StartDate],110)
,EndDate=convert(varchar,edh.[EndDate],110)
FROM [HumanResources].[Employee] e
INNER JOIN [HumanResources].[Employee] m
ON e.ManagerID = m.EmployeeID
INNER JOIN [Person].[Contact] c
ON c.[ContactID] = e.[ContactID]
INNER JOIN [Person].[Contact] cm
ON cm.[ContactID] = m.[ContactID]
INNER JOIN [HumanResources].[EmployeeDepartmentHistory] edh
ON e.[EmployeeID] = edh.[EmployeeID]
INNER JOIN [HumanResources].[Department] d
ON edh.[DepartmentID] = d.[DepartmentID]
INNER JOIN [HumanResources].[Shift] s
ON s.[ShiftID] = edh.[ShiftID];

GO

-- SELECT * FROM [HumanResources].[vEmpDeptHistory] ORDER by Name

 


 

The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page