• sharonsql2013 (8/30/2014)


    I am trying to do an average with the following case statement

    Select

    AVG(Case when TotalHours < = 60 And TotalHours > 0 then TotalHours else 0 end)AVGWithin60

    AVG(Case when TotalHours < = 45 And TotalHours > 0 then TotalHours else 0 end)AVGWithin60

    AVG(Case when TotalHours < = 20 And TotalHours > 0 then TotalHours else 0 end)AVGWithin60

    AVG(Case when TotalHours > 60 And TotalHours > 0 then TotalHours else 0 end)AVGover60

    From

    xyz

    The situation is that the data has lots of rows were TotalHours is Null , So, even if there are two rows say within 60 hours which has avg value as 50 and 55 , instead of showing the average as 52. I get result as 3. Because its doing a count of all rows (Including the NULL ones).How can I fix this situation.

    Quick thought, in a CTE, filter out the NULL values before the aggregation.

    😎