0

I have a table for product, sales_item and a stock with following structure

Product table:

+----+-----+-------------+
| id |name |description  |
+----+-----+-------------+
| 1  |Pr1  |prod1        |
+----+-----+-------------+
| 2  |Pr2  |prod2        |
+----+-----+-------------+
| .. |...  |.....        |
+----+-----+-------------+

sales_item_details table

+-----+----------+------------+-----+
| id  | sales_id | product_id | qty |
+-----+----------+------------+-----+
| 517 |      211 |          1 | 200 |
+-----+----------+------------+-----+
| 518 |      211 |          1 | 120 |
+-----+----------+------------+-----+

and production

+----+------------+-------+
| id | product_id | qty   |
+----+------------+-------+
|  1 |          1 |    20 |
|  2 |          2 |   200 |
|  3 |          1 |    20 |
|  4 |          3 |    30 |
|  5 |          9 |    30 |
|  6 |         65 |    10 |
|  7 |         65 |    50 |
|  8 |         71 |    10 |
|  9 |         71 |    10 |
| 10 |         71 |    10 |
+----+------------+-------+

And now I am creating multiple database with same table defination and need to maintain stock

production table and product table will be maintained from single database only sales_item_details table will be different but product id will same

So how will be the query to get SUM(qty) of sales item details and view the inventory in stock

I have tried this:

SELECT
        `pr`.`id`,
        `pr`.`name`,
        sl.size,
        IFNULL(SUM(s.qty), 0) AS sales,
        IFNULL((SELECT SUM(qty) FROM production st WHERE st.product_id = `pr`.`product-id`), 0) AS stock_added
    FROM products pr
        LEFT JOIN (
            SELECT qty, product_id FROM db1.sales_item_details
            UNION ALL 
            SELECT qty, product_id FROM db2.sales_item_details 
        ) s ON pr.`id` = s.product_id
        LEFT JOIN size_list sl ON sl.id = `pr`.`product-size`
    GROUP BY s.product_id
    ORDER BY sales DESC

but getting the product which is sold

Any help will be appriciated

8
  • Possible Duplicate of this: stackoverflow.com/questions/7432178/… Commented May 23, 2017 at 7:50
  • 1
    'Multiple database' ?? Why ?? Commented May 23, 2017 at 8:02
  • @hiule MySQL does not support the pivot statement. Commented May 23, 2017 at 8:03
  • I do not get the multiple databases design either. Commented May 23, 2017 at 8:04
  • I am creating multiple database having those table but the product and production table will be same as db1 but only sales_item_details table will be different drom db1. Thanks @Shadow Commented May 23, 2017 at 10:16

1 Answer 1

0

1st I created a view holding all sales items grouped by product id in the main database:

CREATE OR REPLACE VIEW unit_sold_all  AS
    SELECT 
        p.`product-id` AS product_id,
        (
            (SELECT IFNULL(SUM(s0.qty), 0) FROM db_1.sales_item_details s0 WHERE s0.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s1.qty), 0) FROM db_2.sales_item_details s1 WHERE s1.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s2.qty), 0) FROM db_3.sales_item_details s2 WHERE s2.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s3.qty), 0) FROM db_4.sales_item_details s3 WHERE s3.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s4.qty), 0) FROM db_5.sales_item_details s4 WHERE s4.product_id = p.`product-id`) 
        ) as total_unit_sales
FROM products p

Then in another sql, I selected the sum of the sales.

PS: I answered this question myself because this might need by another person in the future.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.