【发布时间】:2020-03-28 14:30:16
【问题描述】:
我是 SQL 的基本用户,但需要将 3 个表连接在一起来播种 a) 销售额 b) 退货和 c) 利润
我目前有以下代码
select * from (
select SUM(Return_Amount) , 'Return' as type, monthname(Return_Date) as month_
from returns
group by month_
union
select SUM(Order_Total_Cost) , 'Sales' as type, monthname(Order_Date) as month_
from sales
group by month_
union
select SUM(profit) as profit_ , 'Profit' as type, month_
from(
select sell_price-cost_price as profit , monthname(order_date) month_
from sales
join order_item
on order_item.order_No = sales.order_No
join returns
on returns.order_no = sales.order_No
join supplier
on supplier.Product_ID = order_item.Product_ID
) B group by month_
) A order by month_;
如下图所示:
387 Return August
182 Sales August
867 Profit August
733 Return July
109 Sales July
646 Profit July
596 Return June
我希望它与 Return、Sales 和 Profit 一起显示为单独的列,而不是在一个列中列出所有类型。
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
样本数据和期望的结果真的很有帮助。
标签: sql join mysql-workbench