4

Trying to learn more on sub-query. I am looking for a way to subtract and compare two tables.

  1. Inventory
  2. Sales

My data records are as follows:

Inventory:

mysql> select store_id, product_id, sum(quantity) as inventory from inventories where store_id = 1 group by product_id;
+----------+------------+-----------+
| store_id | product_id | inventory |
+----------+------------+-----------+
|        1 |          8 |        24 |
|        1 |         10 |         4 |
|        1 |         14 |        24 |
+----------+------------+-----------+
3 rows in set (0.00 sec)

Sales

mysql> select store_id, product_id, sum(quantity) as sales from sales where store_id = 1 group by product_id;
+----------+------------+-------+
| store_id | product_id | sales |
+----------+------------+-------+
|        1 |          8 |    12 |
|        1 |         14 |     2 |
|        1 |          8 |     1 |
+----------+------------+-------+
2 rows in set (0.00 sec)

What is the proper sub-query to have the following result?

+----------+------------+-----------+-------+-----------+
| store_id | product_id | inventory | sales | remaining |
+----------+------------+-----------+-------+-----------+
|        1 |          8 |        24 |    12 |        12 |
|        1 |         14 |        24 |     2 |        22 |
|        1 |          8 |        12 |     1 |        11 |
+----------+------------+-----------+-------+-----------+
4
  • This one is similar stackoverflow.com/questions/8217756/… but it groups the product_id. Feb 12, 2015 at 8:54
  • The Sales table is not valid -- You have GROUP BY product_id, yet there are two rows with '8'. Please fix whatever typo caused that.
    – Rick James
    Feb 17, 2015 at 2:05
  • Will You please share your table structure of these two tables so we can easily provide you the better solution. Feb 19, 2015 at 7:29
  • I have to say, this makes no sense. You can not just substract the sales from your inventory and tell: tadaaa, thats what I've got on stock. I dunno if this is a real case scenario, or you only want to learn how to youse subquerys. If your inventory was a snapshot of one moment, you only can determine the acutal number by calculationg (INVENTORY + ALL ADDITIONAL PRODUCTS COMEING IN) - ALL SOLD PRODUCTS. Otherwise you have to work with a date, at least in one of the tables must have Timestasmp added to it. And you have to determine if your using core data or dynamic data (changing)???
    – Clevemayer
    Feb 19, 2015 at 8:14

7 Answers 7

4
+25

You can use results from your queries and join them to calculate the remaining quantity for each product

SELECT 
a.store_id,
a.product_id,
a.inventory,
b.sales,
a.inventory - b.sales AS remaining
FROM (
SELECT store_id, product_id, COALESCE(SUM(quantity),0) AS inventory 
FROM inventories WHERE store_id = 1 
GROUP BY product_id) a
LEFT JOIN (
SELECT store_id, product_id, COALESCE(SUM(quantity),0) AS sales
 FROM sales WHERE store_id = 1 
 GROUP BY product_id ) b USING(store_id, product_id)
1
  • This query tries to sum all inventory, sales, quantities but does not track historical data per sales. Feb 23, 2015 at 15:09
3

To achieve the desired output you need to calculate running totals of product sales. To get meaningful data, the data in sales table must be ordered chronologically. So you need at least one more field to sort data - it doesn't matter if it's a timestamp, or id field. Let's assume there is an id field in sales table. This is a query to get what you described:

SELECT 
    sales.id,
    sales.store_id,
    sales.product_id,
    inventories.quantity-IFNULL(SUM(sales_2.quantity), 0) as inventory,
    sales.quantity as sales,
    inventories.quantity-IFNULL(SUM(sales_2.quantity), 0) - sales.quantity as remaining
FROM
    sales
        INNER JOIN
    inventories ON inventories.store_id = sales.store_id
        AND inventories.product_id = sales.product_id
        LEFT JOIN
    sales AS sales_2 ON sales_2.store_id = sales.store_id
        AND sales_2.product_id = sales.product_id
        AND sales_2.id < sales.id
GROUP BY sales.id , sales.store_id , sales.product_id
ORDER BY sales.id

The second instance of sales table called sales_2 is used to calculate the sum of earlier sales (sales_2.id<sales.id)

You can exclude sales.id from the select clause, but you need to keep it in group by and order by.

2
  • This is exactly what I am trying to achieve, a historical representation of each sales that can determine inventory, sales and remaining items. Thanks! Feb 23, 2015 at 15:10
  • The problem with the query is whenever a new inventory is added, the quantity and remaining becomes negative (-). Need to research more. Feb 25, 2015 at 14:27
2

Try joining the two tables as :

SELECT I.store_id, I.product_id, I.inventory, S.sales, (I.inventory - S.sales) AS remaining
FROM Sales S INNER JOIN INVENTOR I
ON I.store_id = S.store_id
AND I.product_id = S.product_id
6
  • Seems to work, but I tried to add another sales row and from your query, how can we make it to subtract the remaining inventory based from the sales? Feb 10, 2015 at 16:42
  • can you update your data in your question with which you are running?
    – SMA
    Feb 10, 2015 at 16:43
  • Just had it updated, under "What is the proper sub-query to have the following result?". Feb 11, 2015 at 5:56
  • Your query is subtracting the remaining based on the current quantity, it is not retaining the history. Feb 12, 2015 at 8:13
  • 1
    A query is supposed to give information based on current stats. It isn't supposed to retain history.
    – Rohit
    Feb 14, 2015 at 2:07
1

I posted an answer and then re-read what you wanted. I realized that I had mis-read something and I see that you do want sales to be treated as individual transactions that are deducted in sequence i.e. "history". You are still going to need some kind of transaction id or transaction date to determine which order they are to be applied in. That was already noted in another answer too. Perhaps you want to rely on a MySQL row id or something. I don't know enough about MySQL to tell help you with that.

select
    i.store_id, i.product_id,
    i.inventory - s.previous_sales as inventory,
    s.quantity as sales,
    i.inventory - s.previous_sales - s.quantity as remaining
from
    inventories as i inner join
    (
    select store_id, product_id, quantity,
        (
        select sum(quantity)
        from sales as s2
        where
            s2.store_id = s.store_id and s2.product_id = s.product_id
            /* all sales for this store and product prior to this one */
            and s2.[sequencing column] < s.[sequencing column]
        ) as previous_sales
    from sales
    group by store_id, product_id
    ) as s
        on s.store_id = i.store_id and s.product_id = i.product_id
where
    i.store_id = 1

I don't know how you're going to handle changes in your inventory or how far back to look in sales that need to be deducted from inventory. This addresses the problem as you wrote it up.

1

The sample data is quite limited, but I believe we can make these assumptions.

  1. You cannot sell an item unless it has been recorded as inventory
  2. You can have items of inventory you have not sold (yet)

If is not necessary to aggregate inventory then you could use a single "derived table", which is a type of subquery, like this:

    SELECT
          I.store_id
        , I.product_id
        , COALESCE(I.inventory, 0) AS INVENTORY
        , COALESCE(S.sales, 0) AS SALES
        , COALESCE(I.inventory, 0) - COALESCE(S.sales, 0) AS REMAINING
    FROM Inventory I
    LEFT JOIN (
          SELECT
                store_id
              , product_id
              , SUM(sales) AS SALES
          FROM Sales
          WHERE store_id = 1
          GROUP BY
                product_id
    ) S ON I.store_id = S.store_id
                AND I.product_id = S.product_id
    WHERE I.store_id = 1
    ORDER BY
          I.store_id
        , I.product_id
    ;

If it is necessary to also aggregate inventory you could use 2 "derived tables" like this:

SELECT
      I.store_id
    , I.product_id
    , COALESCE(I.inventory, 0) AS INVENTORY
    , COALESCE(S.sales, 0) AS SALES
    , COALESCE(I.inventory, 0) - COALESCE(S.sales, 0) AS REMAINING
FROM (
      SELECT
            store_id
          , product_id
          , SUM(inventory) AS INVENTORY
      FROM Inventory
      WHERE store_id = 1
      GROUP BY
            product_id
) I
LEFT JOIN (
      SELECT
            store_id
          , product_id
          , SUM(sales) AS SALES
      FROM Sales
      WHERE store_id = 1
      GROUP BY
            product_id
) S ON I.store_id = S.store_id
            AND I.product_id = S.product_id
ORDER BY
      I.store_id
    , I.product_id
;

Or you may use "correlated subqueries" within the select clause like this:

SELECT
      I.store_id
    , I.product_id
    , COALESCE((
            SELECT
                  SUM(sales)
            FROM sales S
            WHERE S.store_id = I.store_id
                  AND S.product_id = I.product_id
      ), 0) AS SALES
    , I.inventory - COALESCE((
            SELECT
                  SUM(sales)
            FROM sales S
            WHERE S.store_id = I.store_id
                  AND S.product_id = I.product_id
      ), 0) AS REMAINING
FROM Inventory AS I
WHERE I.store_id = 1
ORDER BY
      I.store_id
    , I.product_id
;

You have asked which is the "proper sub-query". I believe all the above are correct syntactically, but "proper" I don't fully understand. If you mean which will perform best I would suggest derived tables in preference to correlated subqueries, but to arrive at an answer that applies to all situations is near impossible.

To evaluate best performance requires better definition of the data, tables and indexes, then use of execution plans is strongly recommended. It may also be the case that NOT using any subqueries is the best approach (i.e. joins).

see the queries above as a demonstration here: http://sqlfiddle.com/#!9/fa6b6/1

1

The ideal subquery will be the subquery which will use the keys of your table.

For your inventory table, you should have a PRIMARY KEY on (store_id, product_id).

ALTER TABLE inventories ADD PRIMARY KEY (store_id, product_id).

If this is not intended to be unique (in which case, it is not really an inventory, but an "incoming item" table), you can set an INDEX for those two columns.

ALTER TABLE inventories ADD INDEX (store_id, product_id).

You have to define the same index in the sales table

ALTER TABLE sales ADD INDEX (store_id, product_id).

After defining those keys, we can know set the perfect and long performance subquery.

  1. First hypothesis : you have a primary key defined in inventories for (store_id, product_id)

sql:

 SELECT t1.store_id, t1.product_id, t1.quantity inventory,
       IFNULL(sum(t2.quantity), 0) sales,
       t1.quantity-IFNULL(sum(t2.quantity), 0) remaining
     FROM inventories t1
         LEFT JOIN sales t2 ON t1.store_id=t2.store_id
         AND t1.product_id=t2.product_id
      WHERE t1.store_id=1
      GROUP BY t1.store_id, t1.product_id;
  1. Second hypothesis : just an index, things get more complicated

You do the same thing except that instead of querying inventories table, you will replace it with the following subquery:

SELECT store_id, product_id, SUM(quantity) quantity
  FROM inventory
  GROUP BY store_id, product_id;

The result will be the following:

 SELECT t1.store_id, t1.product_id, t1.quantity inventory,
       IFNULL(sum(t2.quantity), 0) sales,
       t1.quantity-IFNULL(sum(t2.quantity), 0) remaining
     FROM 
        (SELECT store_id, product_id, SUM(quantity) quantity
           FROM inventory
           GROUP BY store_id, product_id) t1
       LEFT JOIN sales t2 ON t1.store_id=t2.store_id
       AND t1.product_id=t2.product_id
     WHERE t1.store_id=1
     GROUP BY t1.store_id, t1.product_id;
1

Assuming you have a table of products and a table of stores, then join those together, to get the combination of products and stores. This way a row isn't dependent on a product still being on inventory on a store you are interested in, or the product to have yet been sold from a store you are interested in.

Then LEFT OUTER JOIN sub queries to get the inventory and sales or each product / store.

SELECT b.store_id, a.product_id, IFNULL(c.inventory, 0), IFNULL(d.sales, 0) , (IFNULL(c.inventory, 0) - IFNULL(d.sales, 0)) AS remaining
FROM products a
INNER JOIN stores b
ON b.store_id = 1
LEFT OUTER JOIN 
(
    SELECT store_id, product_id, SUM(quantity) AS inventory 
    FROM inventories 
    WHERE store_id = 1 
    GROUP BY store_id, product_id
) c
ON a.product_id = c.product_id
AND b.store_id = c.store_id 
LEFT OUTER JOIN 
(
    SELECT store_id, product_id, SUM(quantity) AS sales 
    FROM sales 
    WHERE store_id = 1 
    GROUP BY store_id, product_id
) d
ON a.product_id = d.product_id
AND b.store_id = d.store_id 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.