sql-left-outer-join-with rows-based on column value from right table

Sathya

I have two tables Table A and Table B with many-to-one relationship, i.e for every record in Table A, there may be one or multiple rows in Table B. I am Left Outer joining Table A with Table B on columns : A.Employee_Id = B.Employee_Id.

For example, for Employee 1 in Table A, there may be two rows in Table B: first row Event_ID = "R" and second row Event_Id= "S". For Employee 2, there may be only one row with Event_Id ="R".

So basically, I want the rows in Table B to be sorted for every employee based on column value of Event_Id in the order "F", "S" and "R" and then pick the first row from this order, to be joined with Table A.

And I want only one record to be returned in the result for every employee. (Of course, NULL values if there are no records).

Please suggest how can this be done in an optimised way.

Table A Columns:
Employee_Id,  First_Name,  Last_Name

Table B Columns:
Employee_Id, Event_Id, Event_Comment
Multisync

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 (but there still may be several rows for each employer - if tableB has the same EventIds for one employer):

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Left Join - Multiple Rows in Right Table

From Dev

Why LEFT OUTER JOIN reduces rows from left table?

From Dev

Left outer join in SQL with data missing in right table

From Dev

Left join rows and right table rows in same column

From Dev

Which join i want to use to get rows from left table that not found in right table in SQL Server

From Dev

Perform right outer join with a condition for left table

From Dev

Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

From Dev

Left join without multiple rows from right table

From Dev

SQL JOIN get data from left table Only even if all right column match

From Dev

sql left outer join with a constraining column

From Dev

Sql Table Left outer join result Group By

From Dev

left outer join doesnt return all rows on left table

From Dev

Right Outer Join to Left Outer join

From Dev

Count rows from LEFT JOIN where column is value

From Dev

Mysql left join, get all right table columns and 2 columns from left table based on left table max id

From Dev

What is the "left" and "right" table in an intermediate SQL Join?

From Dev

What is the "left" and "right" table in an intermediate SQL Join?

From Dev

How to join the table based on main table column value in SQL Server?

From Dev

SQL JOIN and LEFT OUTER JOIN

From Dev

LEFT JOIN missing values from the right table

From Dev

Sql right outer join ignore all null rows

From Dev

SQL Group rows for every ID using left outer join

From Dev

Laravel 2 table join with all from left table merged with members of the right table with a given FK value

From Dev

How can I create SQL that does a Left Outer Join and also a count from another table?

From Dev

How can I create SQL that does a Left Outer Join and also a count from another table?

From Dev

SQL inner join on a column based on max value from another column

From Dev

SQL Left Outer join with where clause reduces results from left outer join

From Dev

SQL Left Outer join with where clause reduces results from left outer join

From Dev

Filter by value in last row of LEFT OUTER JOIN table

Related Related

  1. 1

    SQL Left Join - Multiple Rows in Right Table

  2. 2

    Why LEFT OUTER JOIN reduces rows from left table?

  3. 3

    Left outer join in SQL with data missing in right table

  4. 4

    Left join rows and right table rows in same column

  5. 5

    Which join i want to use to get rows from left table that not found in right table in SQL Server

  6. 6

    Perform right outer join with a condition for left table

  7. 7

    Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

  8. 8

    Left join without multiple rows from right table

  9. 9

    SQL JOIN get data from left table Only even if all right column match

  10. 10

    sql left outer join with a constraining column

  11. 11

    Sql Table Left outer join result Group By

  12. 12

    left outer join doesnt return all rows on left table

  13. 13

    Right Outer Join to Left Outer join

  14. 14

    Count rows from LEFT JOIN where column is value

  15. 15

    Mysql left join, get all right table columns and 2 columns from left table based on left table max id

  16. 16

    What is the "left" and "right" table in an intermediate SQL Join?

  17. 17

    What is the "left" and "right" table in an intermediate SQL Join?

  18. 18

    How to join the table based on main table column value in SQL Server?

  19. 19

    SQL JOIN and LEFT OUTER JOIN

  20. 20

    LEFT JOIN missing values from the right table

  21. 21

    Sql right outer join ignore all null rows

  22. 22

    SQL Group rows for every ID using left outer join

  23. 23

    Laravel 2 table join with all from left table merged with members of the right table with a given FK value

  24. 24

    How can I create SQL that does a Left Outer Join and also a count from another table?

  25. 25

    How can I create SQL that does a Left Outer Join and also a count from another table?

  26. 26

    SQL inner join on a column based on max value from another column

  27. 27

    SQL Left Outer join with where clause reduces results from left outer join

  28. 28

    SQL Left Outer join with where clause reduces results from left outer join

  29. 29

    Filter by value in last row of LEFT OUTER JOIN table

HotTag

Archive