数据库实验(六)复杂查询

1、在订单明细表中查询订单金额最高的订单。

命令:

select orderNO,sum(quantity*price) as ordersum

from OrderDetail a group by orderNo

having sum(quantity*price)=

(select max(ordersum) from (select orderNO,sum(quantity*price) as ordersum

from OrderDetail group by orderNo)a)

结果截图:

2、 找出至少被订购3次的商品编号、订单编号、订货数量和订货金额,并按订货数量的降序排序输出。

select orderNo,a.productNo,quantity,quantity*price ordersum

from OrderDetail a,(select productNo from OrderDetail group by productNo having COUNT(*)>=3) b

where a.productNo=b.productNo

order by quantity desc

3、查询订单中所订购的商品数量没有超过10个的客户编号和客户名称。

SELECT a.CustomerNo,CustomerName

FROM Customer a

WHERE a.CustomerNo IN ( SELECT CustomerNo

FROM OrderMaster b,OrderDetail c

WHERE b.orderNo=c.orderNo

GROUP BY CustomerNo

HAVING sum(quantity)<10)

4、查找至少订购了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额。

SELECT a.CustomerNo,CustomerName,b.ProductNo,

ProductName,quantity,sum(quantity*price)

FROM Customer a,Product b,OrderMaster c,OrderDetail d

WHERE a.CustomerNo=c.CustomerNo and c.orderNo=d.orderNo

and b.ProductNo=d.ProductNo and

EXISTS (

SELECT CustomerNo

FROM OrderMaster e,OrderDetail f

WHERE e.orderNo=f.orderNo and a.customerNo=e.customerNo

GROUP BY CustomerNo

HAVING count(distinct ProductNo)>=3)

GROUP BY a.CustomerNo,CustomerName,b.ProductNo,

ProductName,quantity

5.查询销售金额最高的销售员编号、订单编号、订单日期和订单金额。

SELECT salerNo,b.orderNo,orderDate, orderSum

FROM Employee a,OrderMaster b

WHERE a.employeeNo=b.salerNo

and orderSum =(select max(orderSum)

from OrderMaster)

6、求每位客户订购的每种商品的总数量及平均单价,并按客户号、商品号从小到大排列。

select customerNo,productNo,sum(quantity) sumquantity,sum(price*quantity)/sum(quantity) avgprice

from OrderDetail a,OrderMaster b

where a.orderNo=b.orderNo

group by customerNO,productNo

order by customerNo,productNo 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值