I am trying to improve query readability by using a function in SQL Server Express 2008. Here is a sample of what I'm trying to do.
I have a table where we store a max temperatures reading per hour of the day, then I want to select all days where the max temperature between 8-10AM was greater than the max temp between 12-2PM
So here is how it's like:
DECLARE @TableA TABLE ([Date] DATE, [Time] TIME(0), HighTemp DECIMAL(6,2));
INSERT @TableA VALUES
('2011-09-10','08:00:00',38.15),
('2011-09-10','09:00:00',38.32),
('2011-09-10','10:00:00',38.17),
('2011-09-10','11:00:00',38.10),
('2011-09-10','12:00:00',38.05),
('2011-09-10','13:00:00',38.15),
('2011-09-10','14:00:00',38.30),
('2011-09-11','08:00:00',38.12),
('2011-09-11','09:00:00',38.09),
('2011-09-11','10:00:00',38.07),
('2011-09-11','11:00:00',38.15),
('2011-09-11','12:00:00',38.13),
('2011-09-11','13:00:00',38.11),
('2011-09-11','14:00:00',38.10),
('2011-09-12','08:00:00',38.30),
('2011-09-12','09:00:00',38.33),
('2011-09-12','10:00:00',38.35),
('2011-09-12','11:00:00',38.30),
('2011-09-12','12:00:00',38.25),
('2011-09-12','13:00:00',38.23),
('2011-09-12','14:00:00',38.20)
select distinct [DATE] from @TableA maintbl
where
-- Select the high temp between 08:00:00-10:00:00
(select MAX(HighTemp) from @TableA tmptbl where tmptbl.Time >= '08:00:00' and tmptbl.Time <= '10:00:00' and maintbl.Date = tmptbl.Date)
>
-- Select the high between 12:00:00-14:00:00
(select MAX(HighTemp) from @TableA tmptbl where tmptbl.Time >= '12:00:00' and tmptbl.Time <= '14:00:00' and maintbl.Date = tmptbl.Date)
The query runs well (fast) and the result for the above query should be: 2011-09-10 2011-09-12
Now, I have tried to simplify the query by using a function that retrieves the max tempreture in a certain day and time period, so the query is easier to read, like so:
select distinct [DATE] from @TableA maintbl
where GetPeriodHigh(maintbl.Date, '08:00:00', '10:00:00') > GetPeriodHigh(maintbl.Date, '12:00:00', '14:00:00')
And the function is like so:
CREATE FUNCTION [dbo].[GetPeriodHigh]
(
@Date date,
@From time,
@To time
)
RETURNS decimal(6,2)
AS
BEGIN
declare @res decimal(6,2)
select @res = MAX(high) from MyTable
where Time >= @from and Time <= @to and Date = @Date
return @res
END
The problem is that running the query using the function takes LOOONG time, actually I never saw it finishes, looks like it is in some sort of infinite loop...
Any ideas why is that, and there is anything I can do to simplify my query?
Thx.