Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-07 : 07:32:56
|
| Hi,I have a te table Cartons that contains carton_id,cartonstatus_id,packlist_idI have another table called Packlist with packlist_id, packlist_statusThe third table cartonStatus contains cartonstatus_id, cartonrankDifferent cartons can have the same packlist and have different statuses.I need to update the packlist status with the highest status according to the carton status.For example carton tablecarton_id cartonstatus_id packlist_id12 2 1234513 2 1234521 4 1234523 1 12345CartonStatus tablecartonstatus_id cartonrank1 32 23 14 5In this case cartonstatus_id 2 has the highest rank (rank 2 after rank 1).I need to update packlist with packlist_statusid =2.I need to run through all the packlist_ids in the packlist table and update their status.How can i do that?ThanksWhisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-07 : 08:34:15
|
| [code]UPDATE pSET p.packlist_status = rnk.rankFROM Packlist pCROSS APPLY (SELECT TOP 1 rank FROM CartonStatus cs INNER JOIN carton c ON c.cartonstatus_id = cs.cartonstatus_id WHERE c.packlist_id = p.packlist_id ORDER BY rank DESC)rnk[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-07 : 09:53:44
|
| Thanks for the help :)Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-07 : 10:26:09
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|