Cumulative distinct count

前端 未结 4 1615
北海茫月
北海茫月 2021-02-06 14:37

I am working on query to get cumulative distinct count of uids on daily basis.

Example : Say there are 2 uids (100,200) appeared on date 2016-11-01 and they also appea

4条回答
  •  鱼传尺愫
    2021-02-06 15:12

    Please try the following...

    SELECT date AS date
           COUNT( uid ) AS daily_cumulative_count
    FROM ( SELECT leftTable.date AS date,
                  rightTable.uid AS uid
           FROM sample_table AS leftTable
           JOIN sample_table AS rightTable ON leftTable.date >= rightTable.date
           GROUP BY leftTable.date,
                    rightTable.uid
         ) AS allUIDSForDateFinder
    GROUP BY date;
    

    This statement starts by joining one instance of sample_table to another in such a way that each record in leftTable has associated with it a copy of each record from rightTable that has an earlier or equal date value. This effectively attaches a list to each date of all uid values that have occurred up to and including that date value.

    The resulting dataset is refined to unique date and uid combinations through use of GROUP BY.

    The refined dataset from the subquery allUIDSForDateFinder is then grouped by date by the main body of the query, and a COUNT() of uid values associated with each group is performed.

    If you have any questions or comments, then please feel free to post a Comment accordingly.

提交回复
热议问题