Incorrect usage of calling Aggregate functions

Source: Internet
Author: User

 

If the query contains an aggregate function and the selected columns are not in the aggregate function, these columns must be in the group by clause; otherwise the following error occurs: ORA-00937: not a single-group function. For example, the following query selects the product_type_id column and AVG (price), but does not use a group by clause containing product_type_id:

SQL> select product_type_id, AVG (price)
2 from products;
Select product_type_id, AVG (price)
*
Error at line 1:
ORA-00937: not a single-group Function

This error occurs because the database does not know how to process the product_type_id column in The result set. Consider: This query attempts to use the AVG aggregate function to operate on multiple rows of records, but tries to obtain the value of the product_type_id column from each row. These two operations cannot be completed at the same time. A group by clause must be provided to notify the database to group the rows in the same product_type_id column. Then, the database can pass the rows in these groups to the AVG function.

Warning:

If the query contains an aggregate function and the selected columns are not in the aggregate function, these columns must be in the group by clause.

Also, you cannot use aggregate functions in the WHERE clause to limit rows. If you do this, the following error occurs: ORA-00934: group function is not allowed here. For example:

SQL> select product_type_id, AVG (price)
2 from products
3 where AVG (price)> 20
4 group by product_type_id;
Where AVG (price)> 20
*
Error at line 3:
The ORA-00934: group function is not allowed here

This error occurs because the WHERE clause can only be used to filter a single row rather than a row group. To filter row groups, you can use the having clause.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.