home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 18,685 of 19,505   
   Erland Sommarskog to Dinesh   
   Re: Need help to update table based on p   
   09 Jul 12 23:43:22   
   
   From: esquel@sommarskog.se   
      
   Dinesh (dinesht15@gmail.com) writes:   
   > Now I need to update above table for RefilLeft column values for each   
   > custID based on their previous refilLeft value with following formula   
   >   
   > RefilLeft = Previous Date RefilLeft - (QTY/WrittenQty)   
   >   
   > First month refil left will be always equals to RefilWritten.   
   >   
      
   This will give you the correct result eventually:   
      
   UPDATE tbl   
   SET    RefillLeft = (SELECT MIN(b.RefilLeft) - SUM(b.Qty/b.WrittenQty)   
                        FROM   tbl b   
                        WHERE  a.CustID = b.CustID   
                          AND  b.Date < a.Date)   
   FROM  tbl a   
      
   However, performance will not be fantastic as it grows with the square of   
   number rows per customer.   
      
   In SQL 2012 there are much more effecient ways to do this.   
      
      
   --   
   Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se   
      
   Links for SQL Server Books Online:   
   SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx   
   SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca