首页 > 解决方案 > 如何在第二个 GROUP BY 语句期间重新汇总值

问题描述

这是我的输出:桌子

正如您在右侧看到的,这些值是从前一个 group by 的 sum() 函数中获得的,但我在表中仍然有非唯一的名称。我怎样才能使表中的所有名称都是唯一的,并且右侧的值对非唯一成员求和。

这是我的查询,以防有帮助:

select alp_customer.first, alp_customer.last, alp_customer.email, sum(alp_orderline.order_price)
from alp_orderline
inner join alp_orders on alp_orderline.order_id = alp_orders.order_id
inner join alp_customer on alp_orders.cust_id = alp_customer.cust_id
group by alp_orderline.order_id

我希望我的问题足够清楚,我对 SQL 很陌生,无法在线找到解决方案。

标签: sqlgroup-by

解决方案


在 group by 中使用 alp_customer.first, alp_customer.last, alp_customer.email这些列

select alp_customer.first, alp_customer.last, alp_customer.email, sum(alp_orderline.order_price)
from alp_orderline
inner join alp_orders on alp_orderline.order_id = alp_orders.order_id
inner join alp_customer on alp_orders.cust_id = alp_customer.cust_id
group by alp_customer.first, alp_customer.last, alp_customer.email

推荐阅读