title: How to do a COUNT(DISTINCT) using window functions with a frame in SQL Server
body: <p>Piggybacking this lovely question:
  <a href="https://stackoverflow.com/questions/11202878/partition-function-count-over-possible-using-distinct">Partition Function COUNT() OVER possible using DISTINCT</a></p>
  <p>I wish to calculate a moving count of distinct value.
  Something along the lines of:</p>
  <p><code>Count(distinct machine_id) over(partition by model order by _timestamp rows between 6 preceding and current row)</code></p>
  <p>Obviously, SQL Server does not support the syntax. Unfortunately, I don't understand well enough (didn't internalize would be more accurate) how that dense_rank walk-around works:</p>
  <pre><code>dense_rank() over (partition by model order by machine_id) 
  + dense_rank() over (partition by model order by machine_id) 
  - 1
  </code></pre>
  <p>and therefore I am not able tweak it to meet my need for a moving window.
  If I order by machine_id, would it be enough to order by _timestamp as well and use <code>rows between</code>?</p>