如何只显示3行?

时间:2017-10-07 06:35:41

标签: mysql sql

我的查询:

Select
    email_address,
    count(customer_id) AS order_count,
    sum((item_price - discount_amount) * (quantity)) AS order_total, 
    avg((item_price - discount_amount) * (quantity)) AS avg_order_total
from customers join orders
using(customer_id)
join order_items
using(order_id)
group by
    customer_id,
    email_address
order by
    avg((item_price - discount_amount) * (quantity)) desc

My output

Expected output

我不确定如何仅显示3个想要的行并制作avg_order_total 四舍五入到小数点后两位。无法使用限制

2 个答案:

答案 0 :(得分:0)

这是你正确的查询!

select
    email_address,
    count(customer_id) AS order_count,
    sum((item_price - discount_amount) * (quantity)) AS order_total, 
    avg((item_price - discount_amount) * (quantity)) AS avg_order_total
from customers join orders
using(customer_id)
join order_items
using(order_id)
group by
    customer_id,
    email_address
order by
    sum((item_price - discount_amount) * (quantity)) desc
limit 3

如果您不能使用限制,那么您只能在应用程序级别读取前三个结果。

答案 1 :(得分:-1)

limit 3添加到您的sql查询中。