1

有人可以帮我弄这个吗。

我有一个表结构如下

Department_name | Owner_ID | Approver_ID
-----------------------------------------
Dept 1          | 1234567  | 1234567

现在我想要基于员工 ID 的输出如下

喜欢

select department_name,Role  where Owner_ID= '1234567' or Approver_ID = '1234567'

Department_name | Role
------------------------- 
Dept 1          | Owner
Dept 1          | Approver

更重要的是,我需要将它作为两个单独的行来获取,如上所示,并且 Role 不是表中的列,其值将是 Approver 或 Owner 基于给定的 ID。

我无法弄清楚如何得到这个。提前致谢。

4

4 回答 4

1

一个简单的解决方案是联合:http ://sqlfiddle.com/#!6/f13c9/5/0

SELECT
  Department_name,
  'Owner'
FROM test
WHERE Owner_ID = 1234567

UNION ALL

SELECT
  Department_name,
  'Approver'
FROM test
WHERE Approver_ID = 1234567
于 2013-05-30T10:21:38.240 回答
1

请试试这个..

SELECT Department_name,
CASE WHEN Role = (SELECT Owner_ID FROM Department) 
THEN 'Owner' ELSE 'Approver'
END AS Role
FROM Department
UNPIVOT(Role for Column1 in(Owner_ID,Approver_ID)) AS Role
于 2013-05-30T10:57:07.343 回答
0

您也可以使用 APPLY 运算符

SELECT t.Department_name, o.Role       
FROM dbo.YourTable t 
  CROSS APPLY (
               VALUES(CASE WHEN t.Owner_ID = '1234567' THEN 'Owner' END),
                     (CASE WHEN t.Approver_ID = '1234567' THEN 'Approver' END)
               ) o(Role)
WHERE o.Role IS NOT NULL
于 2013-05-30T10:20:15.347 回答
0

试试这个

SELECT Department_name , Role FROM TableName UNPIVOT (Role for Department_name in (Owner_ID , Approver_ID)) AS Role

于 2013-05-30T10:23:25.317 回答