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 631
囚心锁ツ
囚心锁ツ 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:40

    The gist of the issue is that there is no such concept as aggregate of an aggregate applied to a relation, see Aggregation. Having such a concept would leave too many holes in the definition and makes the GROUP BY clause impossible to express: it needs to define both the inner aggregate GROUP BY clause and the outer aggregate as well! This applies also to the other aggregate attributes, like the HAVING clause.

    However, the result of an aggregate applied to a relation is another relation, and this result relation in turn can support a new aggregate operator. This explains why you can aggregate the result into an outer SELECT. This leaves no ambiguity in the definition, each SELECT has its own distinct GROUP BY/HAVING clauses.

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