select c.Cust_id, Customer_Name, round(sum(Sales)) as Total_Sales
from market_fact_full m
inner join cust_dimen c using (Cust_id)
group by c.Cust_id
order by Sales desc;
另外,当我添加所有非聚合COL时,仍然会得到以下错误:
select c.Cust_id, Customer_Name, round(sum(Sales)) as Total_Sales
from market_fact_full m
inner join cust_dimen c using (Cust_id)
group by c.Cust_id, Customer_Name
order by Sales desc;
错误代码:1055。 ORDER BY子句的表达式#1不在GROUP BY子句中,并且包含非聚合列'Market_Star_Schema.M.Sales',该列在功能上不依赖于GROUP BY子句中的列; 这与SQL_MODE=ONLY_FULL_GROUP_BY不兼容
这是因为您试图按列sales
order--但是由于您是按客户分组,因此每个组中都有多个
sales
,因此数据库不能很好地确定它应该做什么。
想必,您想要按客户的总销售额对其进行排序。 为此,可以参考from
子句中定义的别名:
select c.cust_id, c.customer_name, round(sum(m.sales)) as total_sales
from market_fact_full m
inner join cust_dimen c using (Cust_id)
group by c.cust_id, c.customer_name
order by total_sales desc;