/*** Declare variables ***/
declare @1count int
declare @11count int
declare @12count int
declare @31count int
declare @32count int
declare @121count int
declare @211count int
declare @212count int
declare @221count int
declare @year_time int
declare @month_time int
declare @day_time int
declare @hour_time int
declare @month_diff int
declare @last_run_date DATE
declare @my_counter int
/*** Get the last time this stored procedure was run ***/
SET @last_run_date = SELECT last_run_date FROM status_table
/*** So the deal is if the last run date was, lets say, 3 months ago, we need to retally ***/
/*** the statistics for those months, as well as the stats so far for this month. ***/
/*** So what we are going to do here is get the difference in months, and then for each of ***/
/*** those months, we are going to get the stats. ***/
/*** The months are also zero based. ***/
IF (MONTH(@last_run_date) <> MONTH(CURRENT_TIMESTAMP))
BEGIN
/*** Set the counter ***/
SET @my_counter = 0
SET @month_diff = DATEDIFF(mm, @last_run_date, GETDATE())
/*** While the counter is less than the difference in months ***/
WHILE (@my_counter < @month_diff)
BEGIN
/*** Get the month for this year ***/
SET @year_time = YEAR(DATEADD(mm, @last_run_date, @my_counter))
SET @month_time = MONTH(DATEADD(mm, @last_run_date, @my_counter))
SET @my_hour = 0
/** For each hour of the day. This is zero based ***/
WHILE (@hour_time < 24)
BEGIN
SET @1count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 1
SET @11count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 11
SET @12count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 12
SET @31count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 31
SET @32count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 32
SET @121count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 121
SET @211count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 211
SET @212count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 212
SET @221count = SELECT COUNT(*) FROM master
WHERE MY_YEAR = @year_time AND MY_MONTH =
@month_time AND MY_HOUR = @hour_time
AND MY_VAR = 221
INSERT INTO _Hourly (MY_YEAR, MY_MONTH, MY_HOUR,
1Count, 11Count, 12Count, 31Count, 32Count,
121Count, 211Count, 212Count, 221Count) VALUES
(@year_time, @month_time, @hour_time, @1count,
@11count, @31count, @32count, @121count,
@211count, @212count, @221count)
@hour_time = @hour_time + 1
END
/*** Increment the counter ***/
@my_counter = @my_counter + 1
END
/*** Update the status table when we are done ***/
UPDATE status_table SET last_run_date = GETDATE()
ELSE
SET @month_time = MONTH(CURRENT_TIMESTAMP)
SET @year_time = YEAR(CURRENT_TIMESTAMP)
SET @my_hour = 0
/*** For all of the hours of the day...zero based ***/
WHILE (@hour_time < 24)
BEGIN
SET @1count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 1
SET @11count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 11
SET @12count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 12
SET @31count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 31
SET @32count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 32
SET @121count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 121
SET @211count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 211
SET @212count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 212
SET @221count = SELECT COUNT(*) FROM master WHERE
MY_YEAR = @year_time AND MY_MONTH = @month_time
AND MY_HOUR = @hour_time AND MY_VAR = 221
INSERT INTO _Hourly (MY_YEAR, MY_MONTH, MY_HOUR,
1Count, 11Count, 12Count, 31Count, 32Count,
121Count, 211Count, 212Count, 221Count) VALUES
(@year_time, @month_time, @hour_time, @1count,
@11count, @31count, @32count, @121count, @211count,
@212count, @221count)
@hour_time = @hour_time + 1
END
UPDATE status_table SET last_run_date = GETDATE()
END