Created
March 18, 2018 02:36
-
-
Save onnayokheng/1aa26a36898bc9e3fc17d9f9217cc87b to your computer and use it in GitHub Desktop.
Query for calculate inventory item from multiple tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
inventories.product_id, | |
products.name as product_name, | |
stock_create.stock_production as total_stock_production, | |
SUM(inventories.qty) as total_stock_in, | |
SUM(distribusi.stock) as total_stock_in_warehouse, | |
SUM(inventories.qty - delivery.stock) as total_stock_current, | |
delivery.stock as total_stock_out | |
FROM inventories | |
LEFT JOIN ( | |
SELECT distributions.product_id, SUM(distributions.qty) as stock | |
FROM inventories, distributions | |
WHERE distributions.product_id = inventories.product_id | |
GROUP BY product_id | |
) as distribusi ON inventories.product_id = distribusi.product_id | |
LEFT JOIN ( | |
SELECT inventories.product_id, SUM(productions.qty) as stock_production | |
FROM inventories, productions | |
WHERE inventories.product_id = productions.product_id | |
GROUP BY product_id | |
) as stock_create ON inventories.product_id = stock_create.product_id | |
LEFT JOIN ( | |
SELECT inventories.product_id, SUM(deliveries.qty) as stock | |
FROM inventories, deliveries | |
WHERE inventories.product_id = deliveries.product_id | |
GROUP BY product_id | |
) as delivery ON inventories.product_id = delivery.product_id | |
LEFT JOIN products ON products.id = inventories.product_id | |
GROUP BY product_id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Query get distributions with products