【问题标题】:How to find Profit for Category如何找到类别的利润
【发布时间】:2013-05-29 01:59:45
【问题描述】:

我的项目是关于一家珠宝店,我试图找出每个产品类别的利润。 让我更具体一些

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

SALES(salesid,productid,quantity,price)

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

RETURN(salesid,productid,date,quantity,price)

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

PROCUREMENT(procurementid,productid,quantity,price)

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

product_category(categoryid,category)

categoryid  category
1           Gold
2           Silver
.
5           Platin

产品(Productid,categoryid)

Productid  categoryid
13001      1
13002      3
.
.
13010      5

利润来自这种类型:

Profit=Quantity*Price(Sell)-Quantity*Price(Return)-Quantity*Price(Procurement)

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

SELECT categoryid,
       category,
       (coalesce(a.rev,0)- coalesce(b.ret,0),
                           coalesce(c.cost,0)) AS profit
FROM product category AS g
    JOIN product AS h ON g.categoryid = h.categoryid
    JOIN
      (SELECT categoryid,
              sum(quantity*price) AS rev
       FROM sales AS a,
            product AS b
       WHERE a.productid = b.productid
       GROUP BY categoryid) a
    LEFT OUTER JOIN
      (SELECT cartegoryid,
              sum(quantity*price) AS ret
       FROM RETURN AS a ,
                      product AS b
       WHERE a.productid = b.productid
       GROUP BY categoryid) b ON a.categoryid = b.categoryid
    LEFT OUTER JOIN
      (SELECT categoryid,
              sum(quantity*price) AS cost
       FROM procurement AS a,
            product AS b
       WHERE a.productid = b.productid
       GROUP BY categoryid) c ON a.categoryid = c.categoryid ,
    product AS d,
    procurement AS e
WHERE MONTH(f.date) = MONTH(e.date)
  AND YEAR(date) = 2013 

[抱歉对齐我是该网站的新手,不知道如何复制粘贴代码(:D)] 不管当我这样做时,它会变成这样的状态

categoryid  category  profit
1           Gold      -100
2           Silver    -100
.
5           Platin    -100

不知道问题出在哪里...我进行了很多更改和切换,但没有任何结果...任何建议都会很有帮助。谢谢您

【问题讨论】:

    标签: mysql sql join sum mysql-workbench


    【解决方案1】:

    最初看起来您的利润公式中有一个额外的逗号。 这个

    (coalesce(a.rev,0) - coalesce(b.ret,0),coalesce(c.cost,0)) as profit
    

    应该是这样的

    coalesce(a.rev,0) - coalesce(b.ret,0) - coalesce(c.cost,0) AS profit
    

    这个查询还有几个问题

    • 在 where 子句之前,加入成本子查询后,添加产品和采购表,但不加入它们。这将导致笛卡尔连接,这会抛弃您的结果。
    • 在 where 子句中,您没有指定要使用哪个表的日期字段。 AND YEAR(date) = 2013 应该是 e.datef.date。如果您尝试运行它,那应该会给您一个错误。
    • WHERE MONTH(f.date) = MONTH(e.date) f.date 指的是哪个表?您没有为任何表指定别名 f
    • 您加入采购并使用其日期字段按月过滤结果,但您的收入、退货和成本子查询总计均未考虑日期。这会影响你的结果。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2012-12-19
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多