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 704
囚心锁ツ
囚心锁ツ 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: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).

提交回复
热议问题