title: SQL - query by dynamic weights
body: <p>I have a database with users that have a list of items.<br>
  Simple design:</p>
  
  <p><strong>Users:</strong></p>
  
  <p>UserStuff | UserID</p>
  
  <p><strong>ItemsList:</strong></p>
  
  <p>itemID | UserID</p>
  
  <p>Each user has a list of items. An Item can belong to many users (same itemID, different userID).</p>
  
  <p>Now I want to give weights to the items dynamically (calculated at runtime, not something constant that can be stored in the database) and then Select the users with the highest weights according to their Items.</p>
  
  <p>Can I give the weight to the items in an SQL query, and then do the calculation ?  </p>
  
  <p>I'm trying to avoid looping through the database and calculating the users' weights.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>example: </p>
  
  <p><strong>UserStuff</strong> | <strong>UserID</strong><br>
  blah1         |  1<br>
  blah2         |  2  </p>
  
  <p><strong>itemID</strong> | <strong>UserID</strong><br>
  1          |    1<br>
  1          |    2<br>
  2          |    1<br>
  3          |    1<br>
  4          |    2<br>
  5          |    1  </p>
  
  <p>wieghts:<br>
  itemId = 1 = 15
  itemID = 2 = 10</p>
  
  <p>UserID1 total weight = 25<br>
  UserID2 total weight = 10</p>