【问题标题】:How to find Profit between 3 different tables如何在 3 个不同的表之间找到利润
【发布时间】:2023-03-14 01:06:02
【问题描述】:

我的项目是关于一家珠宝店,我试图找到利润最高的产品。

我有 3 张桌子可以提供信息:

销售表:

salesid  productid   Quantity Price
11001    13001       4        5
11002    13002       6        10
11003    13003       5        16
.
.
11012    13012       7        15

返回表:

salesid  productid   Quantity Price
11003    13003       1        16
11007    13007       3        12
11008    13008       3        8

采购表:

procurementid  productid   Quantity Price
100001         13001       10       2
100002         13002       10       2
.
. 
100012         13012       10       2

利润由这个公式给出:

利润 = 数量 * 价格(销售)- 数量 * 价格(退货)- 数量 * 价格(采购)

现在问题来了。到目前为止我想出了这个

select a.productid,(a.quantity*a.price-b.quantity*b.price-c.quantity*c.price) as Profit
from sales as a ,return as b ,procurement as c
where a.productid = c.productid 
GROUP BY productid

在这种情况下,我没有得到正确的答案。

这是因为在返回表中我只有 3 个寄存器,但在其他表中我有 12 个,所以当它计算利润时,它会为其他表的每一行使用整个返回表。

我尝试使用max(Profit),但它什么也没做。

我实际上不知道如何连接返回表的 3 个寄存器,以便仅在必须时使用它们。当我尝试连接时,很多行都是空的。我认为必须使用OUTER JOIN 或其他东西来完成某些事情,但我不知道该怎么做。

【问题讨论】:

    标签: mysql sql max mysql-workbench


    【解决方案1】:

    您似乎对 SQL 很陌生。给表格别名时,尽量使用表格缩写。它使查询更易于阅读(例如,p 用于 procurements 用于销售)。

    另外,您需要学习正确的连接语法。但是,这对您并没有真正的帮助,因为您需要在进行连接之前进行预聚合。也就是说,分别获取成本、销售额和退货金额,然后将它们组合在一起:

    select p.productid,
           (coalesce(s.rev, 0) - coalesce(r.ret, 0) - coalesce(p.cost, 0)) as profit
    from (select p.productid, SUM(quantity*price) as cost
          from procurement p
          group by p.productid
         ) p left outer join
         (select s.productid, sum(quantity*price) as rev
          from sales s
          group by s.productid
         ) s 
         on p.productid = s.productid
         (select r.productid, sum(quantity*price) as ret
          from return
          group by s.productid
         ) r
         on p.productid = r.productid;
    

    此查询还使用left outer join。这很重要,因为并非所有产品都可能有销售或退货。您不想失去这些,仅仅因为它们是糟糕的卖家(没有销售)或非常受欢迎(没有退货)。

    【讨论】:

    • 我同意这张海报,应该使用采购表中的产品id。你可以没有销售有采购......
    • 我发现这真的很有帮助..我现在非常接近答案,但问题是:我可以看到列中的所有 productid,但在利润列中我只能查看我在返回表中的 3 个寄存器,其他所有寄存器均为 NULL
    • @user2432398 。 . .使用名为coalesce() 的函数进行了修复。当数据丢失(例如返回)时,该值为 NULL,这会将算术表达式转换为 NULLcoalesce()NULL 转换为 0
    • 我想到这个功能的时间我看到你的回复:) 非常感谢你
    【解决方案2】:

    看起来您需要在退货表上使用 LEFT JOIN,因为它只有 3 个产品,而其他表有 12 个。

    select a.productid,
        (a.quantity*a.price-coalesce(b.quantity,0)*coalesce(b.price,0)-c.quantity*c.price) as Profit
    from sales a 
        join procurement c on a.productid = c.productid 
        left join return b on a.salesid = b.salesid
    group by a.productid
    

    这也使用COALESCENULL 的值更改为0。

    【讨论】:

    • @user2432398 -- np,很高兴我能帮上忙。顺便说一句——我不完全确定你是否需要使用 SUM——如果你的表中只有 12 条记录(即 1-1 关系),那么你不需要任何聚合。但是,如果您的销售表每个产品可能有多个记录,那么请使用 SUM 查看 Gordon 的答案。亲切的问候。
    【解决方案3】:

    您希望将销售与退货进行左外连接。 (你可以有没有退货的销售,但是当你没有销售的时候应该没有退货。)

    然后你想右外连接采购到这个合并的表(因为采购不保证销售,但你不能卖你没有的东西。)

    select c.productid, 
    (a.quantity*a.price-b.quantity*b.price-c.quantity*c.price) as Profit
    from (sales as a LEFT JOIN return as b ON a.productid = b.productid) 
    RIGHT JOIN procurement as c ON a.productid = c.productid
    where a.productid = c.productid 
    GROUP BY productid
    

    这应该没有空值

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      相关资源
      最近更新 更多