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 |  
                                    | omicron777Starting Member
 
 
                                        9 Posts | 
                                            
                                            |  Posted - 2010-09-30 : 13:02:20 
 |  
                                            | I need help on how to update a column in a table getting the max value of a column that is on the same table.Don't mind the dashes, it's for clarity only.TABLE before updateCARD     ---------- CATEGORY ------ INV_COUNT ----- CATEGORY_INV_COUNTPRODUCT1 ---------- 1000 ---------- 0PRODUCT2 ---------- 1001 ---------- 1PRODUCT3 ---------- 1000 ---------- 0PRODUCT4 ---------- 1000 ---------- 5PRODUCT5 ---------- 1002 ---------- 0PRODUCT6 ---------- 1001 ---------- 0PRODUCT7 ---------- 1000 ---------- 2PRODUCT8 ---------- 1000 ---------- 0PRODUCT9 ---------- 1002 ---------- 0TABLE after update (the result that I need)CARD     ---------- CATEGORY ------ INV_COUNT ----- CATEGORY_INV_COUNTPRODUCT1 ---------- 1000 ---------- 0 ------------- 5PRODUCT2 ---------- 1001 ---------- 1 ------------- 1PRODUCT3 ---------- 1000 ---------- 0 ------------- 5PRODUCT4 ---------- 1000 ---------- 5 ------------- 5PRODUCT5 ---------- 1002 ---------- 0 ------------- 0PRODUCT6 ---------- 1001 ---------- 0 ------------- 1PRODUCT7 ---------- 1000 ---------- 2 ------------- 5PRODUCT8 ---------- 1000 ---------- 0 ------------- 5PRODUCT9 ---------- 1002 ---------- 0 ------------- 0I am issuing this query:update table set category_inv_count =(select max(inv_count) from table t1left join table t2 on t1.category = t2.categorywhere t1.category = t2.category)but I get this kind of result, which is incorrect.CARD     ---------- CATEGORY ------ INV_COUNT ----- CATEGORY_INV_COUNTPRODUCT1 ---------- 1000 ---------- 0 ------------- 5PRODUCT2 ---------- 1001 ---------- 1 ------------- 5PRODUCT3 ---------- 1000 ---------- 0 ------------- 5PRODUCT4 ---------- 1000 ---------- 5 ------------- 5PRODUCT5 ---------- 1002 ---------- 0 ------------- 5PRODUCT6 ---------- 1001 ---------- 0 ------------- 5PRODUCT7 ---------- 1000 ---------- 2 ------------- 5PRODUCT8 ---------- 1000 ---------- 0 ------------- 5PRODUCT9 ---------- 1002 ---------- 0 ------------- 5Please help me on how to derive on the correct result. Thanks! |  |  
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2010-09-30 : 13:26:34 
 |  
                                          | update t1set category_inv_count = t2.category_inv_countfrom table as t1join (select CATEGORY, max(INV_COUNT) as category_inv_count from table group by CATEGORY) as t2on t1.CATEGORY = t2.CATEGORY No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  |  
                                    | omicron777Starting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2010-09-30 : 13:44:36 
 |  
                                          | thank you very much sir!it works!i get the correct result as i wanted |  
                                          |  |  |  
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2010-09-30 : 14:59:08 
 |  
                                          | welcome  No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  |  
                                    | maverick240Starting Member
 
 
                                    1 Post | 
                                        
                                          |  Posted - 2011-09-27 : 13:29:06 
 |  
                                          | Can you sum the inv_count filed by category? |  
                                          |  |  |  
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2011-09-28 : 04:36:18 
 |  
                                          | yes you can use SUM() instead of MAX() No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  |  
                                |  |  |  |