Ich habe versucht, einen Ansatz zu entwickeln, der 2008 funktioniert und mehr auf Sets basiert. Folgendes habe ich mir ausgedacht.
Es war zwar komplexer als ich gehofft hatte, aber es könnte ein interessanter Ansatz für Sie sein, einen Vergleich mit Ihrem aktuellen Ansatz für größere Datenmengen anzustellen. Für das, was es wert ist, läuft dieses Skript in ungefähr 15 ms für den bereitgestellten Datensatz auf meinem Computer (gegenüber 75 ms für das ursprüngliche Skript).
Wie andere bereits erwähnt haben, gibt es wahrscheinlich andere bessere Ansätze, wenn Sie 2012+ Fensterfunktionen oder 2014 eine nativ kompilierte Prozedur verwenden konnten. Aber es kann Spaß machen, manchmal darüber nachzudenken, wie man Dinge ohne die neueren Funktionen macht!
http://sqlfiddle.com/#!3/ad2be/7
-- Assign each point a sequential rank within each agent's history
SELECT p.internalID, ROW_NUMBER() OVER (PARTITION BY internalID ORDER BY date) AS recordRank, date
INTO #orderedPoints
FROM points p
-- Sort the data for efficient lookup of potential incentives
ALTER TABLE #orderedPoints
ADD UNIQUE CLUSTERED (internalId, recordRank)
-- Identify a potential incentive for any point that has 9+ points in the following 30 days
SELECT s.internalId, s.recordRank, ROW_NUMBER() OVER (PARTITION BY s.internalId ORDER BY s.recordRank) AS potentialIncentiveRank
INTO #potentialIncentives
FROM #orderedPoints s
JOIN #orderedPoints e
ON e.internalId = s.internalId
AND e.recordRank = s.recordRank + 9
AND e.date < DATEADD(dd, 30, s.date)
-- Sort the data to allow for efficient retrieval of subsequent incentives
ALTER TABLE #potentialIncentives
ADD UNIQUE CLUSTERED (internalId, recordRank)
-- A table to hold the incentives achieved
CREATE TABLE #incentives (internalId INT NOT NULL, recordRank INT NOT NULL)
-- A couple transient tables to hold the current "fringe" of incentives that were just inserted
CREATE TABLE #newlyProcessedIncentives (internalId INT NOT NULL, recordRank INT NOT NULL)
CREATE TABLE #newlyProcessedIncentives_forFromClause (internalId INT NOT NULL, recordRank INT NOT NULL)
-- Identify the first incentive for each agent
-- Note that TOP clauses and aggregate functions are not allowed in the recursive portion of a CTE
-- If that were allowed, this could serve as the anchor of a recursive CTE and the loop below would be the recursive portion
INSERT INTO #incentives (internalId, recordRank)
OUTPUT inserted.internalId, inserted.recordRank INTO #newlyProcessedIncentives (internalId, recordRank)
SELECT internalId, recordRank
FROM #potentialIncentives
WHERE potentialIncentiveRank = 1
-- Identify the next incentive for each agent, stopping when no further incentives are identified
WHILE EXISTS (SELECT TOP 1 * FROM #newlyProcessedIncentives)
BEGIN
-- Transfer the most recently identified incentives, so that we can truncate the table to capture the next iteration of incentives
TRUNCATE TABLE #newlyProcessedIncentives_forFromClause
INSERT INTO #newlyProcessedIncentives_forFromClause (internalId, recordRank) SELECT internalId, recordRank FROM #newlyProcessedIncentives
TRUNCATE TABLE #newlyProcessedIncentives
-- Identify the next incentive for each agent
INSERT INTO #incentives (internalId, recordRank)
OUTPUT inserted.internalId, inserted.recordRank INTO #newlyProcessedIncentives (internalId, recordRank)
SELECT nextIncentive.internalId, nextIncentive.recordRank
FROM #newlyProcessedIncentives_forFromClause f
CROSS APPLY (
SELECT TOP 1 p.*
FROM #potentialIncentives p
WHERE p.internalId = f.internalId
AND p.recordRank >= f.recordRank + 10 -- A new incentive can only start after all 10 points from the previous incentive
ORDER BY p.recordRank
) nextIncentive
END
-- Present the final results in the same format as the original implementation
SELECT a.internalId, a.points, i.incentives
FROM ( -- Tabulate points
SELECT internalId, MAX(recordRank) AS points
FROM #orderedPoints
GROUP BY internalId
) a
JOIN ( -- Tabulate incentives achieved
SELECT internalId, COUNT(*) AS incentives
FROM #incentives
GROUP BY internalId
) i
ON i.internalId = a.internalId