Part IV SQL aggregate functions

Aggregate function simply aggregated data without actually retrieved them, running in the row group, and the calculation function returns a single value.

Example: Returns the number of items in the table of products, the highest price, lowest and average

SQL语句:SELECT COUNT(*) AS num_items, MIN(prod_price) AS price_min, MAX(prod_price) AS price_max,AVG(prod_price) AS price_avg FROM products;

1) AVG () function

The AVG () after passing through the table and calculate the number of rows in a particular column count value, the average value of the column is obtained, can be used to return both columns to the average of all, an average value may be used to return a particular column or row.

Example 1: to return the products table the average price of all products

SQL语句:SELECT AVG(prod_price) AS avg_price FROM products;

Example 2: Back average price offered by a particular vendor

SQL语句:SELECT AVG(prod_price) AS avg_price FROM products WHERE vend_id=1003;

Example 3: Returns the average price of a particular vendor's products, considering only the average of the different prices

SQL语句:SELECT AVG(DISTINCT prod_price) AS avg_price FROM products WHERE vend_id=1003;

Note: AVG () can be used to determine the average value of a particular column, and the column name must be given as a function parameter. In order to obtain an average value of a plurality of columns, you must use a plurality of the AVG () function is additionally used when DISTINCT aggregate functions, column name must be specified, otherwise an error.

(2) COUNT () function

COUNT () function is used to count, can use this function to determine the number of rows or the number of qualifying rows of the table.

Use COUNT (column) having a row of a particular column value is counted, the specified column is empty row is COUNT () function is ignored, but if you use COUNT (*) for counting the number of rows in the table, not ignoring .

Example 1: Returns the total number of customers the customer table

SQL语句:SELECT COUNT(*) AS num_cust FROM customers;

Example 2: only the customer with an email address count

SQL语句:SELECT COUNT(cust_email) AS num_cust FROM customers;

(3) MAX () function

MAX () function returns the maximum specified column, need to specify the function name column, and ignores the row of column value is NULL.

Example: return products price list of the most expensive items

SQL语句:SELECT MAX(prod_price) AS max_price FROM products;

(4) MIN () function

() Function and the function MAX () function-MIN Instead, it returns the minimum value of the specified column, but also need to specify the column name.

Example: return the products table cheapest items

SQL语句:SELECT MIN(prod_price) AS min_price FROM products;

(5) SUM () function

The SUM () function to return the specified value and the column, i.e. the total.

: Example 1 total goods order number 20005 retrieve the ordered items

SQL语句:SELECT SUM(quantity) AS items_ordered FROM orderitems WHERE order_num = 20005;

Example 2: Statistical items Order No. 20005 in total orders for all items (unit price * quantity)

SQL语句:SELECT SUM(item_price*quantity) AS total_price FROM orderitems WHERE order_num=20005;

Guess you like

Origin www.cnblogs.com/wzw0625/p/12607492.html