第12章:汇总数据
P76 avg()函数求平均值
select avg(prod_price) as avg_price from products; #计算prod_price的平均值,并且命名为avg_price#
select avg(prod_price) as avg_price from products where vend_id=1003; #计算priod_id=1003的prod_price的平均值,并且命名为avg_price#
select count(*) as num_cust from customers; #通配符*表示选中所有行,包括空值,返回是行数。#
select count(cust_email) as num_cust from customers; #计算行cust_email的个数,不包括空值#
select max(prod_price) as max_price from products; #max()计算列prod_price的最大数#
P79 min()函数用来计算最小值,经常也计算最初的时间。
select min(prod_price) as min_price from products; #计算prod_price的最小值,并命名为min_price#
select sum(quantity) as items_ordered from orderitems where order_num=2005; #sum()函数计算括号里面的列中订单号码等于20005的数值的总值#
P80
select sum(quantity*item_price) as total_price from orderitems where order_num=2005; #sum()函数计算括号里面的列中订单号码等于20005的数值的总值#
P81 聚集不同值
select avg(distinct prod_price) as avg_price from products where vend_id=1003; #distinct 表示去掉重复值,然后再求去掉重复值之后的平均值,并命名为avg_price ,条件是vend_id=1003#
注意:没有count(distinct)的使用,有distinct(count)的使用。
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; #一个语句中同时出现几个语句#