Is there a standard on SQL implementaton for multiple calls to the same aggregate function in the same query?
For example, consider the following example, based on a popular example schema:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)>1000
Presumably, it takes computation time to calculate the value of SUM(OrderPrice). Is this cost incurred for each reference to the aggregate function, or is the result stored for a particular query?
Or, is there no standard for SQL engine implementation for this case?
Although I have worked with many different DBMS, I will only show you the result of proving this on SQL Server. Consider this query, which even includes a CAST in the expression. Looking at the query plan, the expression
sum(cast(number as bigint))is only taken once, which is defined asDEFINE:([Expr1005]=SUM([Expr1006])).It may not be very obvious above, since it doesn’t show the SELECT result, so I have added a
*10to the query below. Notice that it now includes one extra stepDEFINE:([Expr1006]=[Expr1005]*(10))(steps run bottom to top) which demonstrates that the new expression required it to perform an extra calculation. Yet, even this is optimized, as it doesn’t recalculate the entire expression – merely, it is taking Expr1005 and multiplying that by 10!This is very likely how all the other DBMS work as well, at least considering the major ones i.e. PostgreSQL, Sybase, Oracle, DB2, Firebird, MySQL.