Compare 2 table columns and update new column

meysam motamedi

I have 2 tables like this:

enter image description here

enter image description here

For example in first row from table 2 col value is 3661 and its equal to 'med' from table 1 I want to update class from table 1 with 'MED' and ...

This is the code I wrote but something is wrong :(

UPDATE table1 SET
class = ( CASE 
            WHEN table2.col = table1.med
            THEN  'MED'
            --------------------------------
            WHEN table2.col = table1.mgl
            THEN  'MGL'
            --------------------------------
            WHEN table2.col = table1.rhp
            THEN  'RHP'
            --------------------------------
            WHEN table2.col = table1.epd
            THEN  'EPD'
            --------------------------------
            WHEN table2.col = table1.jpa
            THEN  'JPA'
            --------------------------------
            ELSE  'NULL'
          FROM 
            table1 LEFT outer JOIN table2
            )
Ormoz

I edited your code. you did not mention which database you use, Anyway try this:

   UPDATE table1 
    SET class = ( CASE 
                WHEN table2.col = table1.med
                THEN  'MED'
                --------------------------------
                WHEN table2.col = table1.mgl
                THEN  'MGL'
                --------------------------------
                WHEN table2.col = table1.rhp
                THEN  'RHP'
                --------------------------------
                WHEN table2.col = table1.epd
                THEN  'EPD'
                --------------------------------
                WHEN table2.col = table1.jpa
                THEN  'JPA'
                --------------------------------
                ELSE  'NULL'
    END)
    from table1 left join table2 on table1.number=table2.number

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL: combine 2 columns in one table to compare with 1 column in another table

From Dev

Update table column based on columns in other table

From Dev

compare a single input to 2 table columns

From Dev

Yii2 ActiveRecord when first table updated or add new record, update second table column which is sum of first table column

From Dev

UPDATE 2 columns of a table of values from a table

From Dev

provide column= value with selecting min date column ,compare 2 columns

From Dev

How to write this SQL Server syntax to update a column based on the values of 2 other columns in that same table

From Dev

Compare old and new table and show idifference in the flag column

From Dev

Update a mysql table column with sum of joined table columns value?

From Dev

Split column data for 2 new columns in MYSQL

From Dev

new column based on combination of 2 columns in Spark

From Dev

Create new table from old table and add 2 new columns

From Dev

Compare 2 db table values and update in 1 table

From Dev

Mysql JOIN table and update 2 columns information

From Dev

Mysql JOIN table and update 2 columns information

From Dev

How to iterate through a column in dataframe and update two new columns simultaneously?

From Dev

How to update new column based on the value of existent columns?

From Dev

Compare columns on 2 sheets and return data from another column

From Dev

Compare values in 2 columns and output the result in a third column in pandas

From Dev

Excel formula to compare the 2 columns and extract the value from another column

From Dev

Laravel - update table1 column with table2 column

From Dev

How to compare 2 columns of a table through dynamic querying

From Dev

How to compare Kusto query against 2 table columns?

From Dev

How to compare 2 columns of a table through dynamic querying

From Dev

update table with 4 columns specified, but only 2 columns are available

From Dev

SQLite - Update a column based on values from another table's columns

From Dev

Update a column with the combined results of multiple other columns in the same table

From Dev

Splitting text column into ragged multiple new columns in a data table in R

From Dev

Adding 2 new columns to my table I get "Column count doesn't match value count at row 1"

Related Related

  1. 1

    SQL: combine 2 columns in one table to compare with 1 column in another table

  2. 2

    Update table column based on columns in other table

  3. 3

    compare a single input to 2 table columns

  4. 4

    Yii2 ActiveRecord when first table updated or add new record, update second table column which is sum of first table column

  5. 5

    UPDATE 2 columns of a table of values from a table

  6. 6

    provide column= value with selecting min date column ,compare 2 columns

  7. 7

    How to write this SQL Server syntax to update a column based on the values of 2 other columns in that same table

  8. 8

    Compare old and new table and show idifference in the flag column

  9. 9

    Update a mysql table column with sum of joined table columns value?

  10. 10

    Split column data for 2 new columns in MYSQL

  11. 11

    new column based on combination of 2 columns in Spark

  12. 12

    Create new table from old table and add 2 new columns

  13. 13

    Compare 2 db table values and update in 1 table

  14. 14

    Mysql JOIN table and update 2 columns information

  15. 15

    Mysql JOIN table and update 2 columns information

  16. 16

    How to iterate through a column in dataframe and update two new columns simultaneously?

  17. 17

    How to update new column based on the value of existent columns?

  18. 18

    Compare columns on 2 sheets and return data from another column

  19. 19

    Compare values in 2 columns and output the result in a third column in pandas

  20. 20

    Excel formula to compare the 2 columns and extract the value from another column

  21. 21

    Laravel - update table1 column with table2 column

  22. 22

    How to compare 2 columns of a table through dynamic querying

  23. 23

    How to compare Kusto query against 2 table columns?

  24. 24

    How to compare 2 columns of a table through dynamic querying

  25. 25

    update table with 4 columns specified, but only 2 columns are available

  26. 26

    SQLite - Update a column based on values from another table's columns

  27. 27

    Update a column with the combined results of multiple other columns in the same table

  28. 28

    Splitting text column into ragged multiple new columns in a data table in R

  29. 29

    Adding 2 new columns to my table I get "Column count doesn't match value count at row 1"

HotTag

Archive