title: I need to compute a bunch of per-row values: how can I avoid looping through a table's rows in t-sql
body: <p>I have a table of Facilities, and a table of Transmissions. I want to compute various things  like, facility X sent 10124321409 transmissions in april 2011, of which 9959 were missing SSNs. These are huge tables, so I will store these computed values in a 'cache' table so that they can be accessed easily when a user looks over our system with a UI (ex. selects a date range). I have a bunch of user-defined functions that take a facility name and a date range and compute a value, like this</p>
  
  <pre><code> set @s = dbo.getPctMissingSSNs(facilityX, 1/1/2012, 1/15/2012) //returns pct missing ssn from 1/1/12 to 1/15/12 at facilityX
  </code></pre>
  
  <p>From stackoverflow, I have learned that  </p>
  
  <ol>
  <li>It is a bad idea to loop over a table's rows <a href="https://stackoverflow.com/questions/3910421/what-is-the-best-way-to-loop-over-a-tables-rows">What is the best way to loop over a table&#39;s rows?</a></li>
  <li>It is better to do these sorts of calculations with stored procedures, rather than calling the data from the server to a client, calculating the values, and then uploading the output back to the server's cache.</li>
  </ol>
  
  <p>Is there a way that I can update the cache for each of the facilities, for a bunch of date ranges, without (1) looping over rows in T-SQL (bad) or (2) looping over rows in VB.net to download the value from the server and then upload it right back again (also bad)</p>