我尝试构建具有多个列的子查询。像这样:具有多个列的子查询
--SELF JOIN:
WITH Employees AS
(
SELECT
e.EmployeeID, e.ManagerID, e.Title,
c.FirstName + ISNULL(' ' + c.MiddleName,'') + ' ' + c.LastName AS EmpName
FROM
Employee AS e
INNER JOIN
Contact AS c ON e.ContactID = c.ContactID
)
SELECT
emp.EmployeeID, emp.ManagerID, emp.EmpName, emp.Title AS EmpTitle,
mgr.EmpName as MgrName, mgr.Title as MgrTitle
FROM
Employees AS Emp
INNER JOIN
Employees AS Mgr ON Emp.ManagerID = Mgr.EmployeeID;
--2
WITH Employees AS
(
SELECT
e.EmployeeID, e.ManagerID, e.Title,
c.FirstName + ISNULL(' ' + c.MiddleName,'') + ' ' + c.LastName AS EmpName
FROM
Employee AS e
INNER JOIN
Contact AS c ON e.ContactID = c.ContactID
)
SELECT
EmployeeID, ManagerID, EmpName, Title
FROM
Employees
WHERE
EmployeeID IN (SELECT EmployeeID, er2.MaritalStatus
FROM Employees AS e
INNER JOIN AdventureWorks2012.HumanResources.Employee AS er2 ON e.ManagerID = er2.BusinessEntityID
WHERE er2.MaritalStatus = 'M');
我也想显示MarialStatus
。但我不能做这种方式,因为我得到的错误:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
,但如果我不能做到这一点的子查询存在。所以我的问题是:我可以在子查询中选择多于一列的正确方法是什么?
谢谢
为什么会有人用'tempdb'物理表 –
@Prdp看起来像OP与AdventureWorks的数据实验。 OP? –
?你对OP的含义是什么? – SavantCode