2

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

我有 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或某事,但我不知道该怎么做。

4

3 回答 3

2

You seem to be rather new to SQL. When giving tables aliases, try to use table abbreviations. It makes the queries much easier to read (for example, p for procurement and s for sales).

Also, you need to learn proper join syntax. However, that will not really help you here, because you need to pre-aggregate before doing the joins. That is, get the cost, sales, and return amounts separately and then bring them together:

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;

This query also uses a left outer join. This is important, because not all products may have sales or returns. You don't want to lose these, just because they are lousy sellers (no sales) or supremely popular (no returns).

于 2013-05-29T13:37:06.660 回答
0

you want to do a left outer join of sales to returns. (you can have sales with no returns, but there should be no returns when you have no sales. )

and then you want to right outer join procurement to this comined table (because procurement does not gurantee sales, but you cant sell stuff you dont have. )

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

this should have no nulls

于 2013-05-29T13:37:25.693 回答
0

看起来您需要LEFT JOIN在退货表上使用 a,因为它只有 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。

于 2013-05-29T13:33:59.783 回答