Join Based On Column Value (Best Match)

K09

I have 3 columns to base my JOIN on -> ID, Account, Cust. There can be multiple rows containing the same ID value.

I want to prioritise my JOIN on 1) ID, 2) Account, 3) Cust.

So in the example below, the UserCode that should be populated in @UserData should be 'u11z' as all columns contain a value.

How do I do this? Below my code to date...

UPDATE  @UserData
    SET UserCode = ur.UserCode
FROM @UserData uA 
INNER JOIN UserReference ur
    ON uA.ID = ur.ID
    AND ((ua.Account = ur.Account) OR (ur.Account = ur.Account))
    AND ((ua.Cust = ur.Cust) OR (ur.Cust = ur.Cust))

UserReference TABLE:

Cust       Account      ID       UserCode
234         NULL      9A2346     u12x
234         Test      9A2346     u11z
NULL        NULL      9A2346     u30s

@UserData TABLE:

Cust       Account      ID       UserCode
234         Test      9A2346     NULL

Thanks!

SQLChao

You can try the following. I joined tables, counted the number of matches, and ranked them. Then select rank 1.

; with userCte (userCodeA, userCodeB, rank)
as
(
select a.usercode, b.usercode, 
rank() over (partition by a.id order by case when a.cust = b.cust then 1 else 0 end +
case when a.account = b.account then 1 else 0 end  +
case when a.id = b.id then 1 else 0 end desc) as rank
from userdata a
join userreference b
on a.id = b.id or a.account = b.account or a.id = b.id
)

select * from userCte
--update userCte
--set userCodeA = userCodeB
where rank = 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Join type based on column value

From Dev

MySQL - JOIN based on value of column?

From Dev

Inner Join table based on column value

From Dev

r - copy value based on match in another column

From Dev

Best approach to join selects and setup a column value in MSSQL

From Dev

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

From Dev

Python match a column name based on a column value in another dataframe

From Dev

MySQL - inner join - add column with value based on other value

From Dev

SQL: join on best string match

From Dev

Query to select Single Best Record based on a Column Value

From Dev

how to get only one record based on column value in oracle join

From Dev

Oracle: Is it possible to choose which table to join based on a column value?

From Dev

Return one row in a view based on a left join column value

From Dev

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

From Dev

R - Assign column value based on closest match in second data frame

From Dev

Summing values from a column based on match in another column and first distinct occurrence of value in a third column

From Dev

How to write mysql query to achieve the following type of join (Join based on value of a column )

From Dev

Inner join two files based on one column in unix when row names don't match with sort

From Dev

R - Join on data.table, selecting a different column based on value of another column in row

From Dev

R - create new column dataframe based on index of value match with existing column

From Dev

SQL set column to specified value, based on value match from linked records in a joined table

From Dev

SQL set column to specified value, based on value match from linked records in a joined table

From Dev

Write column based on DataFrame join

From Dev

Mysql join selects multiple column best practice

From Dev

Join MySQL tables based on Partial match of Strings

From Dev

Extract column based on value

From Dev

Join based on the value to different table

From Dev

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

From Dev

Match a value in VBA based on a condition

Related Related

  1. 1

    Join type based on column value

  2. 2

    MySQL - JOIN based on value of column?

  3. 3

    Inner Join table based on column value

  4. 4

    r - copy value based on match in another column

  5. 5

    Best approach to join selects and setup a column value in MSSQL

  6. 6

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

  7. 7

    Python match a column name based on a column value in another dataframe

  8. 8

    MySQL - inner join - add column with value based on other value

  9. 9

    SQL: join on best string match

  10. 10

    Query to select Single Best Record based on a Column Value

  11. 11

    how to get only one record based on column value in oracle join

  12. 12

    Oracle: Is it possible to choose which table to join based on a column value?

  13. 13

    Return one row in a view based on a left join column value

  14. 14

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

  15. 15

    R - Assign column value based on closest match in second data frame

  16. 16

    Summing values from a column based on match in another column and first distinct occurrence of value in a third column

  17. 17

    How to write mysql query to achieve the following type of join (Join based on value of a column )

  18. 18

    Inner join two files based on one column in unix when row names don't match with sort

  19. 19

    R - Join on data.table, selecting a different column based on value of another column in row

  20. 20

    R - create new column dataframe based on index of value match with existing column

  21. 21

    SQL set column to specified value, based on value match from linked records in a joined table

  22. 22

    SQL set column to specified value, based on value match from linked records in a joined table

  23. 23

    Write column based on DataFrame join

  24. 24

    Mysql join selects multiple column best practice

  25. 25

    Join MySQL tables based on Partial match of Strings

  26. 26

    Extract column based on value

  27. 27

    Join based on the value to different table

  28. 28

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

  29. 29

    Match a value in VBA based on a condition

HotTag

Archive