库存计算的总和列

时间:2017-09-15 09:22:40

标签: sql-server

我试图在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引用ID。现在我试图以颜色为基础计算可用库存,即以下格式的购买和销售差异

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

(我没有测试任何东西)

答案 2 :(得分: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