title: FORMAT function exceeding supported size of ORDER BY list of RANGE windows frame (error 8279)
body: <p>When it comes to displaying the year-month of my dates on SQL Server, I like using this solution:</p>
  
  <pre><code>format(year(&lt;my_date&gt;), '0000') + '-' + format(month(&lt;my_date&gt;), '00') as [year-month]
  </code></pre>
  
  <p>And I had nothing to complain about until recently.</p>
  
  <p>But now I need to use that expression with a running total and things are getting trickier.</p>
  
  <p>Here is what the data looks like:</p>
  
  <pre><code>ID |  date
  &gt; 
  &gt; 2514|2018-02-21
  &gt; 
  &gt; 0249|2018-02-21
  &gt; 
  &gt; 5122|2018-01-30
  &gt; 
  &gt; 7410|2018-02-15
  &gt; 
  &gt; 6638|2018-01-07
  </code></pre>
  
  <p>With the expected output:</p>
  
  <pre><code>&gt; running total of count(ID) | year-month
  &gt; 
  &gt; 5|2018-02
  &gt; 
  &gt; 2|2018-01
  </code></pre>
  
  <p>As you can see, I want to get a running total of count(ID) over the whole set.</p>
  
  <p>And here is my code:</p>
  
  <pre><code>SELECT DISTINCT  
  count(id) over (partition by 1 order by format(year(&lt;my_date&gt;), '0000') + '-' + format(month(&lt;my_date&gt;), '00')),
  format(year(&lt;my_date&gt;), '0000') + '-' + format(month(&lt;my_date&gt;), '00')
  
  FROM my_table
  
  ORDER BY format(year(&lt;my_date&gt;), '0000') + '-' + format(month(&lt;my_date&gt;), '00') desc
  </code></pre>
  
  <p>Or, to put it simply:</p>
  
  <pre><code>SELECT DISTINCT  
  count(id) over (partition by 1 order by [year-month]),
  [year-month]
  
  FROM my_table
  
  ORDER BY [year-month] desc
  </code></pre>
  
  <p>But it does not work because I always get the following error message:</p>
  
  <blockquote>
    <p>ORDER BY list of RANGE window frame has total size of 8000 bytes.
    Largest size supported is 900 bytes.</p>
  </blockquote>
  
  <p>My question is therefore twofold:</p>
  
  <ol>
  <li>Why do I get this error message?</li>
  <li>How can I fix this and get the result I want?</li>
  </ol>
  
  <p>EDIT: I removed the second version of my code because I realized it was wrong.</p>