根据不同的where子句从一个创建两列

时间:2016-06-23 15:18:56

标签: sql-server tsql

对于具有不同状态和员工代码值的员工,我的表有两行,我想编写一个查询,根据不同的状态生成1行,其中包含两列员工代码。

以下是表格中的数据

EmployeeId  First_Name  Last_Name  Sex  Employee_Code  Status
----------  ----------  ---------  ---  -------------  ------
  91224       Jane        Scott     F      7754        Active
  91224       Jane        Scott     F      8234       Inactive

这是我的查询,

SELECT ISNULL(e.EmployeeId, '') AS EmployeeId, 
e.First_Name AS FirstName, e.Last_Name AS LastName,
e.Sex AS Gender, 
e1.Employee_Code AS CurrentEmployeeCode, 
e2.Employee_Code AS PreviousEmployeeCode
FROM #tempEmployee e
LEFT JOIN #tempEmployee e1 ON e.EmployeeId = e1.EmployeeId AND e.Status = 'Inactive'
LEFT JOIN #tempEmployee e2 ON e.EmployeeId = e2.EmployeeId AND e.Status = 'Active'
WHERE e.EmployeeId = '91224'

这是我的结果:

EmployeeId  FirstName  LastName  Gender  CurrentEmployeeCode  PreviousEmployeeCode
----------  ---------  --------  ------  -------------------  --------------------
91224         Jane      Scott      F       NULL                      8234
91224         Jane      Scott      F       7754                      NULL

我希望这个employeeId有一行有两列currentemployeecode和previousemployeecode。

4 个答案:

答案 0 :(得分:0)

您可以使用某些聚合和

组执行此操作
SELECT 
    ISNULL(e.EmployeeId, '') AS EmployeeId, 
    e.First_Name AS FirstName, 
    e.Last_Name AS LastName,
    e.Sex AS Gender, 
    MAX(e1.Employee_Code) AS CurrentEmployeeCode, 
    MAX(e2.Employee_Code) AS PreviousEmployeeCode
FROM #tempEmployee e
LEFT JOIN #tempEmployee e1 
    ON e.EmployeeId = e1.EmployeeId 
    AND e.Status = 'Inactive'
LEFT JOIN #tempEmployee e2 
    ON e.EmployeeId = e2.EmployeeId 
    AND e.Status = 'Active'
WHERE e.EmployeeId = '91224'
GROUP BY 
    ISNULL(e.EmployeeId, ''),
    e.First_Name, 
    e.Last_Name,
    e.Sex

我甚至不确定你是否需要链接回原来的表格,给它一个去;

SELECT 
    ISNULL(e.EmployeeId, '') AS EmployeeId, 
    e.First_Name AS FirstName, 
    e.Last_Name AS LastName,
    e.Sex AS Gender, 
    MAX(CASE WHEN e.Status = 'Inactive' THEN e.Employee_Code ELSE 0 END) AS CurrentEmployeeCode, 
    MAX(CASE WHEN e.Status = 'Active' THEN e.Employee_Code ELSE 0 END) AS PreviousEmployeeCode
FROM #tempEmployee e
WHERE e.EmployeeId = '91224'
GROUP BY 
    ISNULL(e.EmployeeId, ''),
    e.First_Name, 
    e.Last_Name,
    e.Sex

包含测试数据的完整代码;

CREATE TABLE #tempEmployee (EmployeeId int, First_Name varchar(4), Last_Name varchar(5), Sex varchar(1), Employee_Code int, Status varchar(8))
INSERT INTO #tempEmployee (EmployeeId, First_Name, Last_Name, Sex, Employee_Code, Status)
VALUES
(91224,'Jane','Scott','F',7754,'Active')
,(91224,'Jane','Scott','F',8234,'Inactive')

SELECT 
    ISNULL(e.EmployeeId, '') AS EmployeeId, 
    e.First_Name AS FirstName, 
    e.Last_Name AS LastName,
    e.Sex AS Gender, 
    MAX(CASE WHEN e.Status = 'Inactive' THEN e.Employee_Code ELSE 0 END) AS CurrentEmployeeCode, 
    MAX(CASE WHEN e.Status = 'Active' THEN e.Employee_Code ELSE 0 END) AS PreviousEmployeeCode
FROM #tempEmployee e
WHERE e.EmployeeId = '91224'
GROUP BY 
    ISNULL(e.EmployeeId, ''),
    e.First_Name, 
    e.Last_Name,
    e.Sex

结果

EmployeeId  FirstName   LastName    Gender  CurrentEmployeeCode PreviousEmployeeCode
91224       Jane        Scott       F       8234                7754

答案 1 :(得分:0)

您需要考虑上下文。由于您只有两个值,因此执行此操作的简单方法就是:

SELECT ISNULL(e.EmployeeId, '') AS EmployeeId, 
e.First_Name AS FirstName, e.Last_Name AS LastName,
e.Sex AS Gender, 
Min(e1.Employee_Code) AS PreviousEmployeeCode
Max(e1.Employee_Code) AS CurrentEmployeeCode
FROM #tempEmployee e
LEFT JOIN #tempEmployee e1 ON e.EmployeeId = e1.EmployeeId 
WHERE e.EmployeeId = '91224'

我也认为新的员工代码会更高。

答案 2 :(得分:0)

你可以用CTE和COALESCE做这样的事情。

;WITH InactiveEmp
AS (
    SELECT EmployeeId
        ,First_Name
        ,Last_Name
        ,Sex
        ,Employee_Code
    FROM #tempEmployee
    WHERE STATUS = 'Inactive'
    )
    ,ActiveEmp
AS (
    SELECT EmployeeId
        ,First_Name
        ,Last_Name
        ,Sex
        ,Employee_Code
    FROM #tempEmployee
    WHERE STATUS = 'Active'
    )
SELECT COALESCE(ia.EmployeeId, a.EmployeeId) AS EmployeeId
    ,COALESCE(ia.First_Name, a.First_Name) AS FirstName
    ,COALESCE(ia.Last_Name, a.Last_Name) AS LastName
    ,COALESCE(ia.Sex, a.Sex) AS Gender
    ,ia.Employee_Code AS CurrentEmployeeCode
    ,a.Employee_CodE AS PreviousEmployeeCode
FROM InactiveEmp ia
FULL JOIN ActiveEmp a
    ON ia.EmployeeId = a.EmployeeID

答案 3 :(得分:0)

你正在检查错误(左)表上的状态 e .Status ='无效'

SELECT distinct 
ISNULL(e.EmployeeId, '') AS EmployeeId, 
e.First_Name AS FirstName, e.Last_Name AS LastName,
e.Sex AS Gender
, e1.Employee_Code AS CurrentEmployeeCode , e1.Status 
, e2.Employee_Code AS PreviousEmployeeCode, e2.Status
FROM #tempEmployee e
LEFT JOIN #tempEmployee e1 ON e.EmployeeId = e1.EmployeeId AND e1.Status = 'Inactive'
LEFT JOIN #tempEmployee e2 ON e.EmployeeId = e2.EmployeeId AND e2.Status = 'Active'
WHERE e.EmployeeId = '91224'