But to get what I need I need that select statement to use the result from a subqury…
SELECT
stock_id
, SUM( qty )
FROM
0_stock_moves
GROUP BY
stock_id
Where the sum for the stock_id is > 0_loc_stock.reorder_level
and I also need to put in this where cluse…
WHERE
0_item_codes.inactive =0
AND 0_item_codes.category_id =7
I just can not seem to put all these peices together…
You only need to SUM (0_stock_moves.qty) to get the current quantity for that stock item, and for that location. 0_loc_stock.reorder_level is just a static number for a stock item and a location. I need to know what stock items, current quantity, for that location is less then the reorder level for those stock items and location.
Beats me why you’d want to confront a quantity with a ‘reorder level’, whatever a ‘reorder level’ is
But, the group by will give you just one ‘reorder level’ from all those present with the given stock id and loc_code = 20. Maybe you should SUM the 'reorder level’s as well?
I don’t think we have one for this table. I did solve the problem be inserting the ( GROUP BY stock_id) clause. I am not sure this is the right way? Also to get what I what I had to add the ( 0_loc_stock.loc_code = 20) in two places. So looks like I could use a little restructuring?
The table you ask about: (0_loc_stock) Field | Type |Collation
loc_code |char(5) |latin1_swedish_ci
stock_id |char(20) |latin1_swedish_ci
reorder_level |bigint(20) |
reorder_qty |bigint(20) |
The code I have so far:
SELECT
0_item_codes.stock_id
, 0_item_codes.description
, SUM(0_stock_moves.qty)
FROM
0_item_codes
JOIN 0_stock_moves
ON 0_item_codes.stock_id = 0_stock_moves.stock_id
WHERE
0_item_codes.inactive = 0
AND 0_item_codes.category_id = 7
AND 0_stock_moves.loc_code = 20
GROUP BY
0_item_codes.stock_id
HAVING SUM(0_stock_moves.qty) >
( SELECT reorder_level
FROM
0_loc_stock
WHERE
0_loc_stock.loc_code = 20
AND 0_loc_stock.stock_id = 0_item_codes.stock_id
GROUP BY
stock_id
)
Thanks for your help, I had to add the loc_code = 20 to both where clauses to get the right result. So as you pointed out, and I now know the group by in subquery is not needed.
But my last question was about restructuring the statement so as to have the loc_code =X stated only one time in the statement.
That’s ok
Recommendations? None, except that there’s no need for the group by in the subquery as far as I can see. Try it without. For the rest it looks good to me
select 0_item_codes.stock_id,
0_item_codes.description,
sum(0_stock_moves.qty)
from 0_item_codes
join 0_stock_moves
on 0_item_codes.stock_id = 0_stock_moves.stock_id
where 0_item_codes.inactive = 0
and 0_item_codes.category_id = 7
group by 0_item_codes.stock_id
having sum(0_stock_moves.qty) >
(select reorder_level
from 0_loc_stock
where 0_loc_stock.stock_id = 0_item_codes.stock_id)
I need a report to use for inventory management. So I set out to learn some SQL. But it will take some time.
This report will tell me what items for that location I need to reorder. The 0_stock_moves table keeps track of item movements. every row in this table is a stock or item movement, the qty column is a negative or positive number. When you add all the qty numbers for that item code you get the current inventory quantity for that item code (stock_id). I need to filter that by location code equals (0_stock_moves.loc_code =X) Then I need to take this total for that stock_id and compare it to the 0_loc_stock table, reorder_level column for current inventory level that is less then reorder level for all stock_id for that location (0_loc_stock.loc_code =X) And then sometime filter that by the category code. I do see a mistake here, I need to go to the table 0_stock_master column category_id to get the category code, And I need filter out inactive items, this column is in two tables 0_item_codes.inactive and 0_stock_master.inactive (inactive =0) I am not sure about table descriptions, but stock_id is the common column.
Ok. But you said that for one stock item id you can have more than 1 row in that table, that’s why you introduced the group by. Right?
I’m asking you if there is a certain logic in these multiple rows? And if there is a certain one of these rows that you want to use in case there are more than one. Or does it make no difference, any of them suits you fine?
The reorder level can be changed to a higher number if you have an item that sells so fast you have a hard time keeping it in stock. When your inventory level drops below the reorder level it would flag for a reorder…