我需要添加一个额外的列,它将获得总销售额%。我试图通过添加:
来实现这一目的Book_sales / SUM(Book_sales ) As 'Total_Sales%'
但是,这不起作用。因为这是一个聚合函数,SQL要求我将publ_id分组,我不想这样做
答案 0 :(得分:1)
我认为您应该使用总计分组查询加入您的查询:
SELECT
....
OD.Quantity*OD.Order_price / SUM_OD.Book_Total As 'Total_Sales%'
....
FROM Books AS BKS
INNER JOIN order_details AS OD ON BKS.book_id=OD.book_id
INNER JOIN
( SELECT book_id,
SUM(Quantity*Order_price) as Book_Total
FROM order_details
GROUP BY book_id
) AS SUM_OD ON BKS.book_id=SUM_OD.book_id