• ScottPletcher (3/29/2013)


    Until you get to SQL 2012, you also have the "quick-and-dirty" version of that, using COMPUTE:

    SELECT Curr_date, Action, AVG(Duration)/1000 as Avg_Duration

    FROM Table_1

    WHERE Curr_date = CAST(GETDATE() as DATE)

    GROUP BY Curr_date, Action

    -- added AVG just to show that multiple COMPUTE functions can be specified :-)

    COMPUTE SUM(AVG(Duration)/1000), AVG(AVG(Duration)/1000)

    It's sometimes easier just to slap on a COMPUTE than to rejigger the query ... and, as so often, in this case it avoids another full scan of the table (COMPUTE's a fun cheat, it died too young!).

    Note that the COMPUTEd value(s) are a separate result set, and do not have any column heading other than the function used to create them, i.e., column is named "sum" or "avg" or "count" or whatever.

    I never even think about COMPUTE. I have seen it before but never really used it. Thanks for the reminder of that long forgotten and neglected feature.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/