连接查询中的分组统计连接查询中的分组统计综合使用连接查询、聚合函数和分组查询可以实现很多复杂的查询需求 的强大功能。例:统计每一家公交公司所属线路的站点总数select count()线路数,sum( number)营运车辆数 from ine_ station LS, Line Lwhere LS. lineno=L. lineno and station=解放大道古田四路在这里插入图片描述统计每条公交线路的司机人数select line No线路号, count)司机数 from driver D, vehicle V, line Lwhere D driverid=V driverid and V. lineld=L. lineld group by lineno orderby司机数desc在这里插入图片描述计算每条公交线路的司机人数,并显示司机人数大于三的分组信息,按司机人数逆序显示。select lineno线路号, count()司机数 from driver D., vehicle V line Lwhere D driverid=V driverid and V. lineld=L. lineld group by linenohaving count()>3 order by司机数desc在这里插入图片描述统计每件商品的销售数量和销售金额,要求按照销售量和销售金额升序显示商品名,商品量和商品金额select g. goodgame商品名,sum(od. quantity)销售量,sum(od. quantityg. unitprice)from goods g left join ordersdetail od on g. goodsld=od. goodsid group by g. goodsidorder by销售量,销售金额在这里插入图片描述计算每个订单的金额,要求按照订单下单日期顺序和订单金额降序显示订单ID,订单下单日期,订单金额和顾客姓名select o. orders订单ID.o. orders Date下单日期, sum(od. quantity"g. unitprice)订单金额C Cname P from orders o, ordersdetail od, goods g, customer c where o. ordersld=od. ordersidand od goodsi D=g. goodsld and ocustomerid=c customerid group by oordersorder by下单日期,订单金额desc
连接查询中的分组
SQL查询技巧:连接查询与分组统计
最新推荐文章于 2025-04-08 10:56:29 发布
本文介绍了如何利用SQL的连接查询、聚合函数和分组统计来解决复杂的数据查询问题。示例包括统计公交公司线路站点数、公交线路司机人数以及筛选司机人数超过三的线路。同时,展示了统计商品销售数量和金额,以及按订单金额和日期排序的查询方法。这些查询技巧对于数据分析和业务理解至关重要。
1434

被折叠的 条评论
为什么被折叠?



