0

我有一个库存表,其中包含特定项目 ID/颜色的总项目数:

库存表

    item_code    |    colour    |    stock
---------------------------------------------
       A         |    red       |    10
       A         |    blue      |    8
       A         |    yellow    |    5
       B         |    red       |    5
       B         |    blue      |    0
       B         |    yellow    |    15

然后我有一个表,其中存储了已订购的商品数量(请注意,可以在不同的订单中找到相同的商品)

订单表

    order_id     |    item_code    |    colour    |    ordered_qty
----------------------------------------------------------------------
       1         |        A        |     red      |        3
       1         |        A        |     blue     |        1
       1         |        A        |     yellow   |        2
       1         |        B        |     red      |        1
       2         |        A        |     red      |        1
       2         |        A        |     yellow   |        3
       2         |        B        |     yellow   |        15

我需要创建一个包含剩余可用库存数量的表:例如,对于库存表中每个项目的数量,我需要从订单表中减去“已订购”数量,以获得如下结果:

剩余表

    item_code    |    colour    |    remaining
------------------------------------------------
       A         |    red       |    6
       A         |    blue      |    7
       A         |    yellow    |    0
       B         |    red       |    4
       B         |    blue      |    0
       B         |    yellow    |    0

我正在考虑使用视图,因为该表将为每个新插入的订单行自动更新。我可以获得每种商品/颜色的“已订购”数量:

SELECT item_code, colour, SUM(stock) as already_placed FROM orders_table GROUP BY item_code, colour

此查询将返回类似的内容(请注意,从未订购过 B/blue):

    item_code    |    colour    |    already_placed
----------------------------------------------------
       A         |    red       |    4
       A         |    blue      |    1
       A         |    yellow    |    5
       B         |    red       |    1
       B         |    yellow    |    15

现在我坚持构建查询,它将在表或另一个视图中为我提供剩余表数量..

长话短说,我需要从原始stock_table中减去已经放置的数量,你能帮帮我吗?我在这里找不到任何“减号”或“减号”功能http://www.sqlite.org/lang_aggfunc.html

有可能吗?

在此先感谢,最好的问候

4

3 回答 3

1

您可以使用相关子查询来计算已订购了多少特定项目:

CREATE VIEW remaining_view AS
SELECT item_code,
       colour,
       stock - (SELECT IFNULL(SUM(ordered_qty), 0)
                FROM orders_table
                WHERE orders_table.item_code = stock_table.item_code
                  AND orders_table.colour    = stock_table.colour)
       AS remaining
FROM stock_table

SQL小提琴

于 2013-08-02T09:19:12.320 回答
1

您可以使用自然左连接:

SELECT item_code, colour, IFNULL(q_stock - q_ordered, 0) as remaining
FROM (SELECT item_code, colour, SUM(stock) as q_stock FROM stock_table GROUP BY item_code, colour)
 NATURAL LEFT JOIN
 (SELECT item_code, colour, SUM(ordered_qty) as q_ordered FROM orders_table GROUP BY item_code, colour)
于 2013-08-02T15:29:11.043 回答
1

您可以 UNION thestock_table和 the orders_table,但在orders_table您更改 的符号ordered_qty。然后你group by

就像是

SELECT item_code, colour, SUM(stock) as remaining FROM
(
    SELECT item_code, colour, stock FROM stock_table
    UNION ALL
    SELECT item_code, colour, -ordered_qty as stock FROM orders_table
)
GROUP BY item_code, colour

SQL 小提琴:http ://sqlfiddle.com/#!7/27155/1

其他选项

SELECT sto.item_code, sto.colour, sto.in_stock - COALESCE(ord.already_placed, 0) AS remaining 
    FROM
        (SELECT item_code, colour, SUM(stock) AS in_stock FROM stock_table GROUP BY item_code, colour) sto
    LEFT JOIN
        (SELECT item_code, colour, SUM(ordered_qty) AS already_placed FROM orders_table GROUP BY item_code, colour) ord
        ON ord.item_code = sto.item_code AND ord.colour = sto.colour

这里SELECTofstoord可以放在两个视图中(一个是订购的总库存,一个是库存的总库存。

SQL 小提琴:http ://sqlfiddle.com/#!7/d78d7/13

于 2013-08-02T08:19:06.660 回答