Partition Function COUNT() OVER possible using DISTINCT

后端 未结 4 1464
臣服心动
臣服心动 2020-11-27 10:26

I\'m trying to write the following in order to get a running total of distinct NumUsers, like so:

NumUsers = COUNT(DISTINCT [UserAccountKey]) OVER (PARTITION         


        
4条回答
  •  臣服心动
    2020-11-27 10:42

    There is a very simple solution using dense_rank()

    dense_rank() over (partition by [Mth] order by [UserAccountKey]) 
    + dense_rank() over (partition by [Mth] order by [UserAccountKey] desc) 
    - 1
    

    This will give you exactly what you were asking for: The number of distinct UserAccountKeys within each month.

提交回复
热议问题