Piggybacking this lovely question: Partition Function COUNT() OVER possible using DISTINCT
I wish to calculate a moving count of distinct value. Something along the lines of:
Count(distinct machine_id) over(partition by model order by _timestamp rows between 6 preceding and current row)
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:
dense_rank() over (partition by model order by machine_id)
+ dense_rank() over (partition by model order by machine_id)
- 1
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 rows between?