按表达式分组查询

时间:2016-05-28 23:54:05

标签: sql oracle group-by

Select Books.Category, Books.Retail,
Round(Orderitems.Quantity * (Books.Retail - Books.Cost),0) AS Category_Profit
From Books
INNER JOIN Orderitems
ON BOOKS.Retail=ORDERITEMS.Paideach
Group by Books.Category, Books.Retail

我要做的是显示每个类别的类别和总利润。涉及两个表

Books Table = Category & Retail & Cost
Orderitems Table= Quantity & Paid each

所以例如Category

Sports = Football, Basketball
Food = Chicken, Beef

结果将显示足球+篮球的零售价格在一个类别中,另一行将显示鸡肉+牛肉作为一个

两个表之间的关系是零售=每个支付 还试图将Category_Profit的值四舍五入到最接近的完整值。

我收到ORA-00979的错误:第2行没有GROUP BY表达式

1 个答案:

答案 0 :(得分:2)

听起来您只需按category分组,而不是retail分组。但是,您需要将sum聚合函数用于其他值:

Select Books.Category, 
       sum(Books.Retail),
       Round(sum(Orderitems.Quantity * (Books.Retail - Books.Cost)),0) AS Category_Profit
  From Books
 INNER JOIN Orderitems
    ON BOOKS.Retail=ORDERITEMS.Paideach
 Group by Books.Category