【MySQL作业】avg 和 count 函数——美和易思聚合函数应用习题

点击打开所使用到的数据库>>>


1、统计所有商品的平均单价、最高单价与平均单价之差、平均单价与最低单价之差。

最高单价与平均单价之差 = max(unitPrice)-avg(unitPrice),平均单价与最低单价之差 = avg(unitPrice)-min(unitPrice):
select avg(unitPrice) 平均单价 , max(unitPrice)-avg(unitPrice) 最高单价与平均单价之差 , avg(unitPrice)-min(unitPrice) 平均单价与最低单价之差 from goods;

2、不采用 avg() 计算所有商品的平均单价。

使用“sum(unitPrice)/count(unitPrice)”计算单价平均值:
select sum(unitPrice)/count(unitPrice) 平均单价 from goods;

3、统计指定客户所下订单的数量和金额。

在客户表查找客户“张宏涛”的客户编号:

select customerID from customer where cName=' 张宏涛 ';
在订单表统计“张宏涛”所下订单的数量和金额:
 
select count(*) 订单数 , sum(amount) 金额 from orders where customerID=4;

 
发布了112 篇原创文章 · 获赞 182 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/weixin_44893902/article/details/105695807
今日推荐