0

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
jasonlcy91
  • 454
  • 6
  • 14

2 Answers2

2

There is no need for a recursive CTE. Just mark the first time a name is seen in a house and use a cumulative sum:

select day, house,
       sum(sum(case when seqnum = 1 then 1 else 0 end)) over (partition by house order by day) as num_unique_names
from (select t.*,
             row_number() over (partition by house, name order by day) as seqnum
      from t
     ) t
group by day, house
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

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
Sai Puli
  • 951
  • 8
  • 12