title: SQL Server: variable window function
body: <p>Using SQL Server 2016, I have two tabels like these:</p>
  
  <pre><code>T1
  -----------------------
  User |  Y   | M | V
  ---- ------------------
  US1  | 2011 | 1 | 87.5
  US1  | 2011 | 2 | 76.4
  ...  | ...  | ..| ...
  US1  | 2018 | 7 | 4.7
  US1  | 2018 | 8 | 7.7
  US1  | 2018 | 9 | 16.3
  US1  | 2018 | 10| 11.1
  US1  | 2018 | 11| 98.8
  US1  | 2018 | 12| 65.7
  US2  | 2011 | 1 | 44.5
  US2  | 2011 | 2 | 34.7
  ...  |  ... |...| ... 
  US2  | 2018 | 6 | 18.7
  US2  | 2018 | 7 | 5.1
  US2  | 2018 | 8 | 1.0 
  US2  | 2018 | 9 | 4.4
  US2  | 2018 | 10| 22.6
  US2  | 2018 | 11| 66.5
  US2  | 2018 | 12| 23.7
  
  T2
  ----------
  User | S | 
  ---- -----
  US1  | 3 | 
  US2  | 5 |
  </code></pre>
  
  <p>I would like to get a running SUM like this</p>
  
  <pre><code>SELECT t1.Y
     ,t1.M
     ,t1.User
     ,SUM(t1.V) OVER (PARTITION BY t1.User ORDER BY t1.Y,t1.M ROWS BETWEEN t2.S PRECEDING AND CURRENT ROW)
  FROM t1
  JOIN t2 ON t1.User = t2.User
  </code></pre>
  
  <p>So my expected Output for the last 2 Months is</p>
  
  <pre><code>US1  | 2018 | 11| 133,9 (98.8+11.1+16.3+7.7)
  US1  | 2018 | 12| 191.9 (65.7+98.8+11.1+16.3)
  US2  | 2018 | 11| 118.3 (66.5+22.6+4.4+1.0+5.1+18.7)
  US2  | 2018 | 12| 123.3 (23.7+66.5+22.6+4.4+1.0+5.1)
  </code></pre>
  
  <p>but I can't use t2.S as variable in the Window Function. </p>
  
  <p>Is there a way to do that using Window Funcitons?</p>