Last active
December 17, 2015 20:29
-
-
Save kristinaconley/5668020 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN | |
SET NOCOUNT ON; | |
/************************************************************* | |
Insert Timeframe 0 - 6 days into table variable. | |
*************************************************************/ | |
DECLARE | |
@integer Int | |
DECLARE @Projected TABLE (timeframe CHAR(10), Successes INT, Contacts INT) | |
SELECT | |
@integer = 0 | |
WHILE @integer < 7 | |
BEGIN | |
INSERT INTO @Projected (timeframe) | |
(SELECT | |
@integer | |
) | |
SET @integer = @integer + 1 | |
END | |
/************************************************************* | |
Calculations. | |
*************************************************************/ | |
BEGIN | |
INSERT INTO @Projected (timeframe, Successes, Contacts) | |
(SELECT | |
P.timeframe | |
,SUM(Successes) AS 'Successes' | |
,SUM(Contacts) AS 'Contact' | |
FROM CalculatedData C | |
RIGHT JOIN @Projected P ON (P.timeframe = datediff(dd,AddedDate,GETDATE())) | |
GROUP BY p.timeframe, datediff(dd,AddedDate,GETDATE()) | |
) | |
END | |
/************************************************************* | |
Results. | |
*************************************************************/ | |
SELECT | |
DISTINCT P.timeframe | |
,* | |
FROM @Projected P | |
INNER JOIN ( | |
SELECT | |
s1.timeframe | |
,RunningTotal = (SELECT SUM(s2.Contacts) | |
FROM @Projected s2 | |
WHERE s2.timeframe <= s1.timeframe) | |
FROM @Projected s1 | |
)P2 ON P.timeframe = P2.timeframe AND P.Contacts IS NOT null | |
ORDER BY 1 | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment