title: How to write a loop as a SQL query efficiently
body: <p>How do I code the following algorithm as a SQL query (<code>a</code> and <code>b</code> and <code>c</code> are are tables in my database)</p>
  
  <pre><code>for all a:
  case c.column=0
  set a.columnname+=b.column1*b.column2
  case c.column=1
  ...
  
  earlier i solved a similar problem by:                          
  UPDATE a 
  set
  a.column= (select SUM(column1) from b where a.column2=b.column2)   
  </code></pre>
  
  <p>but since in is    summing a product of two columns i don't think i can do the same.
  Also the real problem lies with the one to mny relationship a has with c.</p>
  
  <p>relationships:</p>
  
  <p>a one to many b.</p>
  
  <p>b one to one c</p>
  
  <p>lets say A is a table of company data, B is a table of employee data and C tells us if a employee is male or female(just an example not really my problem). Now i need to calculate the total salary given to each employee for each company and store it in a field in the company table.lets say i calculate this differently based on employees gender. Again now there a hundreds of companies and each company has thousands of employees.</p>