SQL Server,按引用数据列排序

时间:2018-01-30 21:09:45

标签: sql-server

我的Employee表包含FirstName列,LastNameDepartmentIdMasterLookup表,字段为Id,{{1} }。我还有一个Category, Value表,它具有以下值。 (请注意,此表包含多个类别的值。即Department和MaritalStatus。)

Department

我想列出Employee表中的所有员工,他们的部门按升序排序,如下所示

1   Department      Human Resources
2   Department      Accounting
3   Department      Operations
4   MaritalStatus   Married
5   MaritalStatus   Single

这是我到目前为止所做的:

FirstName..  Accounting
FirstName... Accounting
FirstName... Human Resources
FirstName... Operations

可以使用动态查询完成吗?

1 个答案:

答案 0 :(得分:0)

  

我想列出Employee表中的所有员工,他们的部门按升序排序

DepartmentName列排序:

SELECT Emp.FirstName, Dept.DepartmentName 
FROM Employee Emp
LEFT OUTER JOIN Department Dept ON Emp.DepartmentId = Dept.DepartmentId
ORDER BY Dept.DepartmentName ASC;