How to use only one matching row of association table to join parents

Kirk Fleming

Question title isn't the best. I have two parent tables A and B and their association table AB. I need columns from A and B, none from AB. For a given row in A, the multiple matching rows in B will always have identical values for the columns I want. Currently, I join the 3 tables and do a DISTINCT on B, but it is incredibly time-consuming. Here's example data:

Table A
    PK_A col1
    1  a
    2  b
    3  b

TAble B    
    PK_B col1
    5  R
    6  S
    7  T
    8  R
    9  R

Table AB    
    FK_A  FK_B
    1    5
    1    8
    1    9

    select
       A.col1,
       B.col1
    from A
    join AB
      on AB.FK_A = A.PK_A
     and AB,FK_B = B.PK_B
    join B
      on B.PK_B = AB.FK_B

returns

a   R
a   R
a   R

I add DISTINCT to get what I want.

Is there a speedier way to do this by forcing a join using only a single matching row in AB? Sort of like where FK_B = MAX(FK_B)?

It will always be the case that the B.col1 values I want for a given value of A.col1 are identical, the unique parameters in the association table aren't of interest in this query.

Hogan

Here is how you would do what you ask, I'm not sure if it would be faster than distinct but it will use just 1 row from the AB table.

select
   A.col1,
   B.col1
from A
join (select 
        FK_A,
        FK_B, 
        row_number() OVER (partition by FK_A ORDER BY FK_B) as rn
      from AB 
     ) jtable ON A.PK_A = jtalbe.FK_A AND rn = 1
join B ON B.PK_B = jtable.FK_B

How this works:

I use the row_number() OVER to "pull out" each row we want to join on from the joining table in a sub-query.

This may be faster than distinct but probably is dependent on what indexes you have defined relative size of tables etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

left join tables, use only one row from left table

From Dev

How to join only one row in joined table with postgres?

From Dev

How to join data to only the first matching row with {data.table} in R

From Dev

How do I map an association through a model's parents if it can only belong to one of them?

From Dev

How conditional join if first matching has no row, use second matching

From Dev

How to return only one row from the right-most table using mysql join

From Dev

Join two tables and return only one row from second table

From Dev

How to reload a value in only one row of table

From Dev

How to make only one row to be expanded in table?

From Dev

MyBatis collection in association only return one row

From Dev

How to search a table using JS and return only the matching row?

From Dev

SQL JOIN to Only One Row

From Dev

Inner join return only the first matching Row

From Dev

Use sqlalchemy to select only one row from related table

From Dev

Return only one of the matching columns of a JOIN

From Dev

JOIN applies to only one table?

From Dev

Table row parents() not working

From Dev

Displaying join table row in view (has_many through association)

From Dev

How do I join the most recent row in one table to most recent row in another table (oracle)

From Dev

How to make expandale only one row in ui-grid table

From Dev

Oracle : How to have only one row active in a table?

From Dev

MySQL - How to select only two columns from a row in one table

From Dev

How to update only one row in table but with id number

From Dev

SQL Join Table on either matching row or all if second table is empty

From Dev

Join one row only over three tables

From Dev

MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table

From Dev

LEFT JOIN show first row from first table only one time

From Dev

MySql LEFT JOIN returns only one NULL row from the other table

From Dev

Laravel 4 - Join table but only latest row

Related Related

  1. 1

    left join tables, use only one row from left table

  2. 2

    How to join only one row in joined table with postgres?

  3. 3

    How to join data to only the first matching row with {data.table} in R

  4. 4

    How do I map an association through a model's parents if it can only belong to one of them?

  5. 5

    How conditional join if first matching has no row, use second matching

  6. 6

    How to return only one row from the right-most table using mysql join

  7. 7

    Join two tables and return only one row from second table

  8. 8

    How to reload a value in only one row of table

  9. 9

    How to make only one row to be expanded in table?

  10. 10

    MyBatis collection in association only return one row

  11. 11

    How to search a table using JS and return only the matching row?

  12. 12

    SQL JOIN to Only One Row

  13. 13

    Inner join return only the first matching Row

  14. 14

    Use sqlalchemy to select only one row from related table

  15. 15

    Return only one of the matching columns of a JOIN

  16. 16

    JOIN applies to only one table?

  17. 17

    Table row parents() not working

  18. 18

    Displaying join table row in view (has_many through association)

  19. 19

    How do I join the most recent row in one table to most recent row in another table (oracle)

  20. 20

    How to make expandale only one row in ui-grid table

  21. 21

    Oracle : How to have only one row active in a table?

  22. 22

    MySQL - How to select only two columns from a row in one table

  23. 23

    How to update only one row in table but with id number

  24. 24

    SQL Join Table on either matching row or all if second table is empty

  25. 25

    Join one row only over three tables

  26. 26

    MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table

  27. 27

    LEFT JOIN show first row from first table only one time

  28. 28

    MySql LEFT JOIN returns only one NULL row from the other table

  29. 29

    Laravel 4 - Join table but only latest row

HotTag

Archive