问题
I have a stock table that holds the number of total items for a specific item id/colour:
stock_table
item_code | colour | stock
---------------------------------------------
A | red | 10
A | blue | 8
A | yellow | 5
B | red | 5
B | blue | 0
B | yellow | 15
then I have a table where already ordered items quantities are stored (note that the same item can be found in different orders)
orders_table
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
I need to create a table that holds the remaining stock quantities available: for example for each item's qty in the stock table, I need to subtract the "already ordered" qty from orders table, to get something like this:
remaining_table
item_code | colour | remaining
------------------------------------------------
A | red | 6
A | blue | 7
A | yellow | 0
B | red | 4
B | blue | 0
B | yellow | 0
I was thinking of using a view, as the table will auto update for every new inserted order row. I'm able to get the "already ordered" quantities for each item/colour:
SELECT item_code, colour, SUM(stock) as already_placed FROM orders_table GROUP BY item_code, colour
this query will return something like (note that B/blue has never been ordered):
item_code | colour | already_placed
----------------------------------------------------
A | red | 4
A | blue | 1
A | yellow | 5
B | red | 1
B | yellow | 15
Now I'm stuck on building the query that will give me, in a table or in another view, the remaining_table quantities..
long story short, I need to subtract from the original stock_table the already_placed quantities, can you please help me? I can't find any "minus" or "subtract" function here http://www.sqlite.org/lang_aggfunc.html
is it possible to do?
Thanks in advance, best regards
回答1:
You can use a correlated subquery to compute how many specific items have been ordered:
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 Fiddle
回答2:
You could UNION the stock_table
and the orders_table
, but in the orders_table
you change the sign of ordered_qty
. Then you group by
Something like
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 Fiddle: http://sqlfiddle.com/#!7/27155/1
Other option
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
Here the SELECT
of sto
and ord
could be put in two views (one with the total of the stock ordered and one with the total of the stock in stock.
SQL Fiddle: http://sqlfiddle.com/#!7/d78d7/13
回答3:
You can use natural left join:
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)
来源:https://stackoverflow.com/questions/18011962/sqlite-calculate-remaining-item-quantities