聚集函数
运行在行组上,计算和返回单个值的函数
avg()
返回所有或特定行或列的平均值
select avg(prod_price) as avg_price
from products;
avg()忽略列值为null的行
count()
1.count(*)对表中行的数目进行计数,不管表列中包含的值是否为空
2.count(column)忽略null值
select count(cust_email) as num_cust from customers;
仅对cust_email不为空的行计数
max()
1.返回指定列中的最大值
2.对非数值数据使用max() 在用于文本数据时,如果数据按相应的列排序,max()返回最后一行
忽略null
min()
sum()
返回全部/指定列值之和
也可用来合计计算值
select sum(item_price*quantity) as total_price from orderitems
where order_num = 2005;
聚集不同值
1.对所有行执行计算,指定all(通常all是默认行为)
2.只包含不同的值,distinct
组合聚集函数
select count(*) as num_items,
min(prod_price) as price_min,
max(prod_price) as price_max
from products;