SQL Server:如何从表中选择其他格式?

时间:2010-03-23 16:04:50

标签: sql-server-2005 select

我有表A.如何从表A中选择以获得表B格式?

Table A

Employee Name Effective Date FieldType     FieldValue
Maung Mya           1/1/2005 Title         Project Engineer
Maung Mya           1/1/2005 Department    IT Department
Maung Mya           1/1/2007 Title         Assist Project Manager
Kyaw Kyaw           1/1/2006 Title         Software Engineer
Kyaw Kyaw           1/1/2006 Department    IT Department

Table B

Effective Date Employee Name Title                  Department
1/1/2007       Maung Mya     Assist Project Manager IT Department
1/1/2006       Kyaw Kyaw     Software Engineer      IT Department

3 个答案:

答案 0 :(得分:2)

看起来你想要这样的东西:

SELECT A.[Effective Date],
       A.[Employee Name],
       A.FieldValue AS Title,
       A2.FieldValue AS Department
FROM A INNER JOIN
     A AS A2
     ON A.[Employee Name] = A2.[Employee Name] AND
        A.[Effective Date] = A2.[Effective Date] AND
        A.FieldType = 'Title' AND
        A2.FieldType = 'Department'
WHERE A.[Effective Date] = (
    SELECT MAX([Effective Date])
    FROM A AS A3
    WHERE A3.[Employee Name] = A.[Employee Name] AND A3.FieldType = 'Title')
    AND A2.[Effective Date] = (
    SELECT MAX([Effective Date])
    FROM A AS A4
    WHERE A4.[Employee Name] = A2.[Employee Name] AND A3.FieldType = 'Department')

答案 1 :(得分:2)

编辑:RedsDevils我编辑了我的代码,并使用您的示例对其进行了测试。这应该现在可以使用

请记住,同一名员工可能会更换部门,或在职业生涯中获得晋升,我的两个联盟会为每种类型寻找最新生效日期

SELECT  derived.[Effective Date], 
        derived.[Employee Name],
        title1.[FieldValue] AS Title, 
        dept1.[FieldValue] AS Department
FROM 
    (
        SELECT MAX([Effective Date]) AS [Effective Date], [Employee Name] 
        FROM TableA
        GROUP BY [Employee Name] 
    ) derived

    -- Inner Join for TITLE
    INNER JOIN TableA title1 ON derived.[Employee Name] = title1.[Employee Name]
    AND title1.FieldType = 'Title'
    AND title1.[Effective Date] = (SELECT MAX(title2.[Effective Date]) 
                                FROM TableA title2 
                                WHERE title1.[Employee Name] = title2.[Employee Name] 
                                AND title2.FieldType = 'Title') 

    -- Inner Join for DEPARTMENT
    INNER JOIN TableA dept1 ON derived.[Employee Name] = dept1.[Employee Name]
    AND dept1.FieldType = 'Department'
    AND dept1.[Effective Date] = (SELECT MAX(dept2.[Effective Date]) 
                                FROM TableA dept2 
                                WHERE dept1.[Employee Name] = dept2.[Employee Name] 
                                AND dept2.FieldType = 'Department') 

我首先创建了一个派生的表来获取MAX生效日期和员工名称,并将其与名称“derived”别名。然后我创建了加入以获得最后知道的标题和部门。

答案 2 :(得分:0)

是的,我明白了!根据{{​​3}},我可以检索我想要的格式,只有2条记录。谢谢大家!答案如下。

SELECT  TableA.[Effective Date],  TableA.[Employee Name],  TableA.FieldValue AS Title,A2.FieldValue AS Department FROM TableA INNER JOIN  TableA AS A2 ON TableA.[EmployeeName] = A2.[EmployeeName] AND  TableA.[EffectiveDate] = (SELECT MAX([EffectiveDate]) FROM TableA AS A3 WHERE A3.[EmployeeName] = TableA.[EmployeeName] AND A3.FieldType = 'Title') AND  TableA.FieldType = 'Title' AND  A2.FieldType = 'Department'