Search code examples
sqlrecursive-queryrecursive-cte

How to count distinct a field cumulatively using recursive cte or other method in SQL?


Using example below, Day 1 will have 1,3,3 distinct name(s) for A,B,C respectively. When calculating distinct name(s) for each house on Day 2, data up to Day 2 is used. When calculating distinct name(s) for each house on Day 3, data up to Day 3 is used.

Can recursive cte be used?

Data:

Day House Name
1 A Jack
1 B Pop
1 C Anna
1 C Dew
1 C Franco
2 A Jon
2 B May
2 C Anna
3 A Jon
3 B Ken
3 C Dew
3 C Dew

Result:

Day House Distinct names
1 A 1
1 B 1
1 C 3
2 A 2 (jack and jon)
2 B 2
2 C 3
3 A 2 (jack and jon)
3 B 3
3 C 3

Solution

  • Without knowing the need and size of data it'll be hard to give an ideal/optimal solution. Assuming a small dataset needing a quick and dirty way to calculate, just use sub query like this...

    SELECT p.[Day]
        , p.House
        , (SELECT COUNT(DISTINCT([Name]))
            FROM #Bing
            WHERE [Day]<= p.[Day] AND House = p.House) DistinctNames
    FROM #Bing p
    GROUP BY [Day], House
    ORDER BY 1