sql-left-outer-join-with rows-基于右表的列值

时间:2014-12-08 20:11:23

标签: sql left-join

我有两个表Table ATable B many-to-one关系,即对于Table A中的每条记录,Table B中可能有一行或多行。我Left Outer加入Table A Table B列{:1}}。A.Employee_Id = B.Employee_Id

例如,对于Employee 1中的Table ATable B中可能有两行:第一行Event_ID = "R"和第二行Event_Id= "S"。对于Employee 2,可能只有一行Event_Id ="R"

基本上,我希望Table B中的行按照“{”,“S”和“R”的顺序,根据Event_Id的列值为每个员工排序,然后选择此订单的第一行,与Table A合并。

我希望每个员工在结果中只返回一条记录。 (当然,如果没有记录,则为NULL值)。

请建议如何以优化的方式完成此操作。

Table A Columns:
Employee_Id,  First_Name,  Last_Name

Table B Columns:
Employee_Id, Event_Id, Event_Comment

2 个答案:

答案 0 :(得分:0)

你没有给出一个架构,所以在这里稍微强调一下,但下面的内容应该有效:

      SELECT CASE WHEN B.EVENTID = 'F' THEN '1'
                  WHEN B.EVENTID = 'S' THEN '2'
                  ELSE '3' END AS ORDER,
      A.EmployeeId,
      B.EventId 
        FROM TableA AS A
        LEFT JOIN TableB AS B
        ON A.EmployeeId = B.EmployeeId
        GROUP BY A.EmployeeId,B.EventId
        Order by A.EmployeeId DESC
      Where ORDER = '1'

答案 1 :(得分:0)

DB with row_number()

select a.Employee_Id, a.First_Name, a.Last_Name,
       b.Event_Id, b.Event_Comment   
from TableA a
     left join
     (select Employee_Id, Event_Id, Event_Comment,
             row_number() over(partition by Employee_Id 
                               order by case Event_ID 
                                        when 'F' then 1 
                                        when 'S' then 2 
                                        when 'R' then 3 end) as rw
    ) b
    on a.Employee_Id = b.Employee_Id
where rw is null or rw = 1; 

Ansi SQL(但每个雇主可能还有几行 - 如果tableB对一个雇主有相同的EventIds):

select a.Employee_Id, a.First_Name, a.Last_Name,
       tmp.Event_Id, tmp.Event_Comment
from 
tableA a 
 left join 
 (select Employee_Id, Event_Id, Event_Comment 
  from TableB b
        join (select Employee_Id, min(case Event_ID 
                                     when 'F' then 1 
                                     when 'S' then 2 
                                     when 'R' then 3 end) as min_event
            from TableB group by Employee_Id
           ) t 
       on t.Employee_Id = b.Employee_Id
          and t.min_event = case b.Event_ID 
                             when 'F' then 1 
                             when 'S' then 2 
                             when 'R' then 3 end
 ) tmp                                       
 on a.Employee_Id = tmp.Employee_Id;