2017-09-15 146 views
1

我正试图计算股票在SQL中。我有3桌产品,购买和销售。 产品表股票计算总和列

+----+------------------+ 
| id | product_name  | 
+----+------------------+ 
| 1 | apple   | 
|----|------------------| 
| 2 |banana   | 
|----|------------------| 
| 3 |mango    | 
+----+------------------+ 

现在颜色表

+----+------------------+ 
| id | color_name  | 
+----+------------------+ 
| 1 | dark   | 
|----|------------------| 
| 2 | light   | 
+----|------------------+ 

购表人是

+-------+-------------+ 
| id | quantity |color 
+-------+-------------+ 
| 1 | 15  |dark 
+-------+-------------+ 
| 1 | 10  |light 
+-------+-------------+ 
| 2 | 5   |dark 
+-------+-------------+ 
| 3 | 25  |light 
+-------+-------------+ 

,销售表

+-------+-------------+ 
| id | quantity |color 
+-------+-------------+ 
| 1 | 5   |dark 
+-------+-------------+ 
| 1 | 5   |light 
+-------+-------------+ 
| 2 | 5   |dark 
+-------+-------------+ 
| 3 | 5   |light 
+-------+-------------+ 

采购和销售表有外键ID参考ences产品表的id。现在我想要计算购买和销售以下形式的现货供应,即区别颜色的基础上,也

+----+------------------+-------------+ 
| id | product_name  | quantity |color 
+----+------------------+-------------+ 
| 1 | apple   | 10  |dark 
|----|------------------|-------------| 
| 1 | apple   | 5  |light 
|----|------------------|-------------| 
| 2 |banana   | 0  |dark 
|----|------------------|-------------| 
| 3 |mango    | 20  |light 
+----+------------------+-------------+ 

回答

2

您可以找

SELECT p.id,p.Name,Purchase.purchaseQty-sales.salseQty as totalQty 
FROM Product p 
OUTER APPLY(
     SELECT purchase.id, SUM(purchase.quantity) purchaseQty 
     FROM purchase 
     where purchase.id= p.id 
     GROUP BY purchase.id 
)Purchase 
OUTER APPLY(
     SELECT sales.id, SUM(sales.quantity) salseQty 
     FROM sales 
     where sales.id= p.id 
     GROUP BY sales.id 
)sales 
+0

如果我们有另一个表颜色会是怎样最好查询颜色的基础上计算的股票太像可用数量红色的苹果和浅红色的苹果 –

+0

我已经改变了这个问题你可以检查它吗 –

0
SELECT a.id, a.product_name, 
SUM(b.quantity) - SUM(c.quantity) as 'quantity' 
FROM product a 
LEFT JOIN purchase b 
ON a.id=b.id 
LEFT JOIN sales c 
ON b.id = c.id group by a.id, a.product_name 

(我没有测试任何东西)

+0

如果某些产品没有销售记录,该怎么办?这将是空如果没有提供如何解决呢 –

+0

有针对的SQL方法,但我不记得(它替换空0) – alexay68

+0

得到它:SELECT ISNULL(c.quantity,0)。语法取决于您使用的数据库的类型 – alexay68

0

试试这个解决方案与外部适用。

drop table if exists dbo.tProduct; 
drop table if exists dbo.tPurchase; 
drop table if exists dbo.tSale; 

create table dbo.tProduct (
    id int 
    , product_name varchar(100) 
); 

create table dbo.tPurchase (
    id int 
    , quantity int 
); 

create table dbo.tSale (
    id int 
    , quantity int 
); 

insert into dbo.tProduct (id, product_name) 
values (1, 'apple'), (2, 'banana'), (3, 'mango'); 

insert into dbo.tPurchase (id, quantity) 
values (1, 15), (2, 10), (1, 5), (3, 25); 

insert into dbo.tSale (id, quantity) 
values (1, 5), (3, 10), (1, 5), (3, 5), (2, 5); 

select 
    p.id 
    , p.product_name 
    , pur.Quantity - sal.Quantity as Quantity 
from dbo.tProduct p 
    outer apply (
     select 
      sum(tp.quantity) as Quantity 
     from dbo.tPurchase tp 
     where p.id = tp.id 
    ) pur 
    outer apply (
     select 
      sum(tp.quantity) as Quantity 
     from dbo.tSale tp 
     where p.id = tp.id 
    ) sal 
+0

我改变了我的问题你可以检查它吗 –