Why can't I perform an aggregate function on an expression containing an aggregate but I can do so by creating a new select statement around it?

后端 未结 7 630
囚心锁ツ
囚心锁ツ 2020-12-09 02:48

Why is it that in SQL Server I can\'t do this:

select  sum(count(id)) as \'count\'
from    table

But I can do

select sum(x.         


        
相关标签:
7条回答
  • 2020-12-09 03:28

    It's working for me using SQLFiddle, not sure why it would't work for you. But I do have an explanation as to why it might not be working for you and why the alternative would work...

    Your example is using a keyword as a column name, that may not always work. But when the column is only in a sub expression, the query engine is free to discard the name (in fact it probaly does) so the fact that it potentially potentially conflicts with a key word may be disregarded.

    EDIT: in response to your edit/comment. No, the two aren't equivalent. The RESULT would be equivalent, but the process of getting to that result is not at all similar. For the first to work, the parser has do some work that simply doesn't make sense for it to do (applying an aggregate to a single value, either on a row by row basis or as), in the second case, an aggregate is applied to a table. The fact that the table is a temporary virtual table will be unimportant to the aggregate function.

    0 讨论(0)
  • 2020-12-09 03:29

    Microsoft SQL Server doesn’t support it.

    You can get around this problem by using a Derived table:

    select sum(x.count)
    from
    (
        select  count(id) as 'count'
        from    table   
    ) x
    

    On the other hand using the below code will give you an error message.

    select  sum(count(id)) as 'count'
    from    table
    

    Cannot perform an aggregate function on an expression containing an aggregate or a subquery

    0 讨论(0)
  • 2020-12-09 03:30

    In simple terms, aggregation functions operate over a column and generate a scalar value, hence they cannot be applied over their result. When you create a select statement over a scalar value you transform it into an artificial column, that's why it can be used by an aggregation function again.

    Please note that most of the times there's no point in applying an aggregation function over the result of another aggregation function: in your sample sum(count(id)) == count(id).

    0 讨论(0)
  • 2020-12-09 03:36

    i would like to know what your expected result in this sql

    select  sum(count(id)) as 'count'
    from    table
    

    when you use the count function, only 1 result(total count) will be return. So, may i ask why you want to sum the only 1 result.

    You will surely got the error because an aggregate function cannot perform on an expression containing an aggregate or a subquery.

    0 讨论(0)
  • 2020-12-09 03:36

    I think you can write the sql query, which produces 'count' of rows for the required output. Functions do not take aggregated functions like 'sum' or aggregated subquery. My problem was resolved by using a simple sql query to get the count out....

    0 讨论(0)
  • 2020-12-09 03:37

    SUM() in your example is a no-op - SUM() of a COUNT() means the same as just COUNT(). So neither of your example queries appear to do anything useful.

    It seems to me that nesting aggregates would only make sense if you wanted to apply two different aggregations - meaning GROUP BY on different sets of columns. To specify two different aggregations you would need to use the GROUPING SETS feature or SUM() OVER feature. Maybe if you explain what you want to achieve someone could show you how.

    0 讨论(0)
提交回复
热议问题