Question

I have three tables, inventory_location, item, item_stock

inventory_location :

inv_loc_id (PK)
inv_loc_desc

item :

item_id (PK)
code_no
item_desc
inv_cat_id

item_stock :

item_stock_id (PK)
item_id
inv_loc_id
quantity

I want to display a report like this :

Code    Description    Loc1    Loc2    Total

1          Desc1            5          3          8

inventory_location has data : (Loc1,Loc2,Loc3,Loc4,Loc5) & can be added with another record.

My problem is, if a location is not yet in item_stock - meaning no item stored in it yet, it will display like the following :

Code    Description    Loc1    Loc2    null    null    Total

1          Desc1            5          3          0          0          8

What I need is, to display all the locations even if they are not yet in item_stock.

Here's my query, hopefully you could correct me.

SELECT 

   il.`inv_loc_id`     AS inv_loc_id, 
   il.`inv_loc_desc`   AS inv_loc_desc, 
   i.`item_id`         AS item_id, 
   i.`nrc_no`          AS nrc_no, 
   i.`code_no`         AS code_no, 
   i.`item_desc`       AS item_desc, 
   i.`unit_id`         AS unit_id, 
   iss.`item_stock_id` AS item_stock_id, 
   iss.`inv_loc_id`    AS inv_loc_id, 
   iss.`quantity`      AS quantity, 
   i.`inv_cat_id`      AS inv_cat_id 

FROM   `item` i 

       LEFT JOIN `item_stock` iss 
              ON iss.`item_id` = i.`item_id` 
       LEFT JOIN `inventory_location` AS il 
              ON il.`inv_loc_id` = iss.`inv_loc_id` 

WHERE  i.inv_cat_id = 1 

GROUP  BY iss.inv_loc_id, 
          i.item_id 

ORDER  BY iss.item_stock_id DESC 
Was it helpful?

Solution

FROM inventory_location il
 LEFT JOIN item_stock iss ON iss.inv_loc_id = il.inv_loc_id
 LEFT JOIN item i ON iss.item_id = i.item_id

Change your FROM clause so that you start with inventory_location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top