【问题标题】:Joining 3 tables in SQL showing all 3 tables在 SQL 中连接 3 个表,显示所有 3 个表
【发布时间】: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


【解决方案1】:

为什么不加入他们?

SELECT x.month_
     , x.returns_
     , y.sales_
     , z.profit_
FROM
  (SELECT SUM(Return_Amount) AS returns_
        , monthname(Return_Date) AS month_
   FROM RETURNS
   GROUP BY month_) x
INNER JOIN
  (SELECT SUM(Order_Total_Cost) AS sales_
        , monthname(Order_Date) AS month_
   FROM sales
   GROUP BY month_) y ON x.month_ = y.month_
INNER JOIN
  (SELECT SUM(profit) AS profit_
        , month_
   FROM
     (SELECT sell_price-cost_price AS profit
           , monthname(order_date) month_
      FROM sales
      INNER JOIN order_item ON order_item.order_No = sales.order_No
      INNER JOIN RETURNS ON returns.order_no = sales.order_No
      INNER JOIN supplier ON supplier.Product_ID = order_item.Product_ID) B
   GROUP BY month_) z ON x.month_ = z.month_

【讨论】:

    【解决方案2】:

    您可以将三个unioned 查询转换为子查询并加入它们。这假定每个子查询总是每月产生一条记录。

    另外:您可能希望加入月份和年份,以防您的数据跨越多年(最终发生在任何实时数据集中)。我还建议使用数字年份和月份(由year()month() 返回),这将产生比月份名称更有效的连接。

    select 
        r.month_name,
        r.total_return_amount,
        s.total_sales,
        p.total_profit
    from 
        (
            select 
                year(return_date) yr,
                month(return_date) mh,
                monthname(return_date) month_name
                sum(return_amount) total_return_amount, 
            from returns
            group by 
                year(return_date), 
                month(return_date),
                monthname(return_date)
        ) r
        inner join (
            select 
                year(order_date) yr,
                month(order_date) mh,
                sum(order_total_cost) total_sales
            from sales
            group by
                year(order_date),
                month(order_date)
        ) s on s.yr = r.yr and s.mh = r.mh
        inner join (
            select 
                year(order_date) yr,
                month(order_date) mh,
                sum(sell_price - cost_price) as total_profit , 
            from sales
            inner join order_item on order_item.order_no = sales.order_no 
            inner join returns on returns.order_no = sales.order_no 
            inner join supplier on supplier.product_id = order_item.product_id
            group by
                year(order_date),
                month(order_date)
        ) p on p.yr = r.yr and p.mh = r.mh
    

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多