title: Sum column total based on criteria sql
body: <p>I am not even sure if I am asking this question correctly. Algorithmically I know what I want to do, but don't know the appropriate syntax in SQL. </p>
  
  <p>I have created a table that contains total online session times by customer number, IP, session start time, and total session length. Here is an example of what this table looks like(ip and CustNo is masked, also not sure how to make tables so excuse the weirdness): </p>
  
  <pre><code>CustNo  minDate                  maxDate                  ClientIp  timeDiff
  123456  2017-11-14-02:39:27.093  2017-11-14-02:39:59.213  1.1.1.1   0.000372
  </code></pre>
  
  <p>I then create another table looking for a specific type of activity and want to know how long this specific user has used that IP for before this specific activity. The second table contains each activity as a separate row, customerID, IP and a timestamp. </p>
  
  <p>Up to here no issue and the tables look fine. </p>
  
  <p>I now need to write the part that will look into the first table based on customer ID and IP, then sum all usage of that IP for that customer as long as session min start time is less than the activity time but I have no idea how to do this. Here is the current function (not working obviously). I am doing a left join because it is possible this will be a new IP and it may not be in the first table. </p>
  
  <pre><code>SELECT 
      *, 
      SUM(##finalSessionSums.timeDiff) 
  FROM 
      ##allTransfersToDiffReceip
  LEFT JOIN 
      ##finalSessionSums ON ##allTransfersToDiffReceip.CustNo = ##finalSessionSums.CustNo 
                         AND ##allTransfersToDiffReceip.ClientIp = ##finalSessionSums.ClientIp 
                         AND ##allTransfersToDiffReceip.[DateTime] &lt; ##finalSessionSums.minDate 
  </code></pre>
  
  <p>I get an aggregate function error here but I don't know how to approach this at all. </p>