views:

40

answers:

1

I have the following query:

SELECT AVG(val) from floatTable
WHERE tagindex IN(1,2,3,4)
AND DateAndTime > '$first_of_year'

It returns the average value for all the values measured for those four tags for the year to date. Since I'm already retrieving this data, how can I get the data since the first of the month, since the first of the week and since midnight? I already have those dates calculated as $first_of_month, $first_of_week and $midnight. I'm trying to minimize the query and was hoping someone could help me whip up some SQL magic to return this data in a single query or an optimized set of queries. This query takes on average 300 seconds, so I want to do it as little as possible.

Thank you in advance.

+5  A: 
SELECT AVG(case when DateAndTime > '$first_of_year' then val end) as FirstOfYear,
       AVG(case when DateAndTime > '$first_of_month' then val end) as FirstOfMonth,
       AVG(case when DateAndTime > '$first_of_week' then val end) as FirstOfWeek,
       AVG(case when DateAndTime > '$midnight' then val end) as Midnight
from floatTable 
WHERE tagindex IN(1,2,3,4) 
    and DateAndTime > '$first_of_year' 

To improve performance, make sure you have indexes on columns DateAndTime and tagIndex.

RedFilter
That works great... how come it works so much faster than my original query?
It shouldn't be any faster than the query in your question, but it probably won't be any (much) slower either, despite adding in the extra columns that you need. When you refer to "original query", do you mean the one in your question or did you have one to get all of the other averages that you wanted? It could be an issue of caching - you've already run a query so SQL Server has that data in memory.
Tom H.
@user438199 Please verify that you mean > I think you might mean >= '$first_of_year'
Nathan Koop
note that $first_of_week might be before $first_of_year in January, except this week will be considered as last week in December, because you only consider days after $first_of_yeear which actually might be January 1st. This depends on your cultural requirements though.
Vash
Thank you everyone.@Nathan Koop >= would be technically correct, although due to the large amount of data being returned, the effect is negligible.
@user438199, if this is the correct answer, you should mark it as such. To do this click the checkmark.
Nathan Koop
@Vash: good point, this can be compensated for by changing last line of query to `and (DateAndTime > '$first_of_year' or DateAndTime > '$first_of_week')`
RedFilter