sgmunson - Monday, June 26, 2017 12:28 PM
Thanks to the PK on both the DT and Visitor columns in the originally defined problem, it's not actually like real life at all because there's nothing to stop a Customer (or whatever) from visiting more than once a day in real life. 😉
Heh... I do appreciate the code but a report that takes a minute 9 to solve just 10,000 rows would be a real problem for me even if I only had to run it once a month because I know that 10,000 rows will be just the tip of the iceberg in the future.
I'll take a look and see what else can be done. Just as a bit of info, I can tell you that it won't involve an rCTE that increments a date by 1 day. A well written WHILE loop can beat those pretty easily. Again, if you haven't already read it, please see the following article for why you shouldn't use rCTEs that "count by one" even for low row counts.
Hidden RBAR: Counting with Recursive CTE's
As a teaser from that article, here's a performance chart of an rCTE vs. 3 other methods of counting. The rCTE is the Red skyrocket on the left.
--Jeff Moden
Change is inevitable... Change for the better is not.