Mysql 查询以检索用户信息谁';的购物车大于所有用户的平均值
我需要编写一个查询来获得一个客户列表,其中所有订单的总和大于所有客户的平均值。我不允许使用子查询Mysql 查询以检索用户信息谁';的购物车大于所有用户的平均值,mysql,sql,group-by,Mysql,Sql,Group By,我需要编写一个查询来获得一个客户列表,其中所有订单的总和大于所有客户的平均值。我不允许使用子查询 select id_client, surname, id_order, data, count(*) as position_t, sum(shop_quantity) as quantity, sum(shop_quantity*d.price) as summa_order, (select avg(shop_quantity*d.price) from shopping_cart join
select id_client, surname, id_order, data, count(*) as position_t, sum(shop_quantity) as quantity,
sum(shop_quantity*d.price) as summa_order, (select avg(shop_quantity*d.price) from shopping_cart join product d on id_product = fk_product) as average
from client a
join `order` b on id_client = fk_client
join shopping_cart c on id_order = fk_order
join product d on id_product = fk_product
join catalog e on id_catalog = fk_catalog
group by id_order
having summa_order > average
order by summa_order desc;
它应该是这样的,但是没有子查询。怎么写?
输出在这里:
注意:其他聚合函数也应起作用(求和、计数)您可以使用聚合函数:
select
id_client
, surname
, id_order
, data
, count(*) as position_t
, sum(shop_quantity) as quantity
, SUM(shop_quantity*d.price) as summa_order
, AVG(shop_quantity*d.price) OVER ( PARTITION BY shopping_cart.id_product) AS average
from client a
join `order` b
on id_client = fk_client
join shopping_cart c
on id_order = fk_order
join product d
on id_product = fk_product
join catalog e
on id_catalog = fk_catalog
group by id_order
having summa_order > average
order by summa_order desc;
我没有更改GROUPBY,但您需要GROUPBY语句中select的所有非聚合列