Update SQL Server Table Row by row from values of other table with joins

Sakthivel

I have 3 tables.

Table Product

Product_ID | Review_date |
1          | 01/01/2018    |
2          | 01/01/2018    |
3          | 01/01/2018    |
4          | 01/01/2018    |

Table Inventory
Inventory_ID  | Product_ID  | Location_ID
1             |        2    | 1    |
2             |        2    | 3    |
3             |        3    | 4    |
4             |        1    | 4    |

Table Location
Location_ID| Review_date |
1          | 04/02/2018    |
2          | 06/03/2018    |
3          | 01/05/2018    |
4          | 08/28/2018    |

UPDATE The product table set of product information. The inventory table has information about places where the products are available, One product can have multiple inventories and a product can have no inventories. The location table has unique list of all the possible locations. The review date in the location table is often updated.

I want to update the review date in the product table for each product ID and selecting the max(review_date) from location table for each product ID. Because a product can have multiple inventories and locations assigned to it. I want the recent date the product's location is updated.

Expected result

Table Product

Product_ID | Review_date |
1          | 08/28/2018    |  this prod id in inventory has loc id 4. 
2          | 04/02/2018    |  two inv records for the product so max date
3          | 08/28/2018    |
4          | 01/01/2018    |  no inv record. so leave it as such


UPDATE  P
SET     P.review_date = L.Inventory_review_date
FROM    Product AS P
CROSS APPLY
        (
        select  top 1 inventory_review_Date
        from    Location as L, Inventory as I, PRODUCT as P
        where   L.Location_ID = I.Inventory_ID and P.Product_ID = I.Product_ID
        order by
                L.Inventory_Review_date desc
        ) as L  

I tried something like this in different ways but i dont seem to get it. Any help appreciated. TIA

user3407753

It looks like you're joining the location table to the inventory table on two different pieces of informaiton. (location id and product id) If LocationID in the Inventory table is a location ID and not a date (as in your example), try this. (Not tested)

UPDATE  P
SET     P.review_date = L.Inventory_review_date
FROM    Product AS P
CROSS APPLY
        (
        select  top 1 inventory_review_Date
        from    Location as L, Inventory as I, PRODUCT as P
        where   L.Location_ID = I.Location_ID and P.Product_ID = I.Product_ID
        order by
                L.Inventory_Review_date desc
        ) as L  

Also, I would think that you are going to have to order by Location_ID to get all locations together, then choose the top date. I haven't tried it, so the aggregate function of TOP might not let you do this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Postgres / SQL row headers from other table

From Dev

SQL - Get row count of table from a few joins away

From Dev

SQl: Update table with last row from a select

From Dev

how to copy some values of a row from a table to an other table?

From Dev

How can i select row from table according to column values of row in sql server

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

Sql select row if no row exists in other table

From Dev

update sql table current row

From Dev

Update data in SQL Table row by row

From Dev

Read SQl table row by row and update a cell

From Dev

Update record in sql table with a sum of values from other table

From Dev

How to make values in row appear as NULL if they are not in other table in SQL

From Dev

Update Each Exisiting Row of SQL Server Table Using Loop

From Dev

How to update table row from values determined within controller

From Dev

Update a row if a column value is not the same on other table

From Dev

Insert new SQL row only if there is same value from other table

From Dev

Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

From Dev

Update table with values from other table

From Dev

Update table with values from other table

From Dev

Update a table based on values from a other table

From Dev

SQL Server previous and next date from a million row table

From Dev

How to get elements from table by row number in sql server

From Dev

How to sum a specific row from one table to another in SQL Server

From Dev

Fetch single row from two rows of a table with join in SQL Server

From Dev

Select corresponding to row from the same table SQL Server

From Dev

How to delete a row with a condition in other table (sql)

From Dev

php UPDATE table row by row

From Dev

Table row cell values update by using row number

From Dev

How to update the nth row in a SQL database table?

Related Related

  1. 1

    Postgres / SQL row headers from other table

  2. 2

    SQL - Get row count of table from a few joins away

  3. 3

    SQl: Update table with last row from a select

  4. 4

    how to copy some values of a row from a table to an other table?

  5. 5

    How can i select row from table according to column values of row in sql server

  6. 6

    how to select one row from one table and multiple rows from other table using joins in mysql,

  7. 7

    Sql select row if no row exists in other table

  8. 8

    update sql table current row

  9. 9

    Update data in SQL Table row by row

  10. 10

    Read SQl table row by row and update a cell

  11. 11

    Update record in sql table with a sum of values from other table

  12. 12

    How to make values in row appear as NULL if they are not in other table in SQL

  13. 13

    Update Each Exisiting Row of SQL Server Table Using Loop

  14. 14

    How to update table row from values determined within controller

  15. 15

    Update a row if a column value is not the same on other table

  16. 16

    Insert new SQL row only if there is same value from other table

  17. 17

    Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

  18. 18

    Update table with values from other table

  19. 19

    Update table with values from other table

  20. 20

    Update a table based on values from a other table

  21. 21

    SQL Server previous and next date from a million row table

  22. 22

    How to get elements from table by row number in sql server

  23. 23

    How to sum a specific row from one table to another in SQL Server

  24. 24

    Fetch single row from two rows of a table with join in SQL Server

  25. 25

    Select corresponding to row from the same table SQL Server

  26. 26

    How to delete a row with a condition in other table (sql)

  27. 27

    php UPDATE table row by row

  28. 28

    Table row cell values update by using row number

  29. 29

    How to update the nth row in a SQL database table?

HotTag

Archive