Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
CraigYoungman
Starting Member
6 Posts |
Posted - 2012-05-03 : 09:13:20
|
-- I’m trying to create a script to update min/max levels for an inventory system. -- I want to look at at specified period in ticket history. Then using various bits -- of information (lead time, safety stock, etc) calculate the min/max stocking levels for selected items.-- Below are the tables with the relevant columns:VI_PS_TKT_HIST_LIN -- historical sales historyItem_no tkt_dat -- date of ticketqty_sold_stk_unit -- qty of item soldIM_ITEM -- Item tableItem_no categ_cod -- item categoryvendor -- vendor item is purchased fromIM_INV -- inventory specific info (each store or warehouse)Item_no Loc_id -- location (system supports multi warehouse)min_qty -- min qty before reordermax_qty -- max qty to stockVENDOR_ITEM -- Vendor spcific item detialsItem_no Vendor -- primary vendormin_ord_qty -- min qty to order from vendorI'm new and having trouble when multiple tables get involved. So far I have this. It works as far as it goes. But it does not yet take into account selecting which items to include based on vendor or item category (from the IM_ITEM table). It also does not take into account vendor minimum order qtys.-- Declare var used in scriptdeclare @days intdeclare @fdate datedeclare @tdate datedeclare @mindays intdeclare @maxdays intdeclare @leaddays intdeclare @forloc varchar(15)declare @vendor varchar(15)declare @cat varchar(15)-- Set the variables below ---set @fdate = '01/01/2011 00:00:00' -- Starting date in historyset @tdate = '12/31/2011 23:59:59' -- Ending date in history set @mindays = '5' -- Minimum days of stockset @maxdays = '8' -- Maximum days of stockset @leaddays = '3' -- # of days from placing order to receiving orderset @forloc = 'MAIN' -- For which inventory locationset @vendor = ‘HP’ -- Item vendor (only update items for this vendor)set @cat = 'PRINTER' -- Item category (only update items from this category) -- calculate number of days between datesset @days = datediff(day, @fdate, @tdate) + '1'update im_invset max_qty = (select ceiling((@leaddays + @maxdays ) * sum(b.qty_sold_stk_unit) / @days)from vi_ps_tkt_hist_lin as b where b.item_no = im_inv.item_no) where exists (select * from vi_ps_tkt_hist_lin as b where b.item_no=im_inv.item_no and b.lin_typ in ('S', 'R') and b.stk_loc_id = @forloc and b.tkt_dat between @fdate AND @tdate) update im_invset min_qty = (select ceiling((@leaddays + @mindays ) * sum(b.qty_sold_stk_unit) / @days)from vi_ps_tkt_hist_lin as b where b.item_no = im_inv.item_no) where exists (select * from vi_ps_tkt_hist_lin as b where b.item_no=im_inv.item_no and b.lin_typ in ('S', 'R') and b.stk_loc_id = @forloc and b.tkt_dat between @fdate AND @tdate) |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-03 : 15:36:40
|
please post some sample data from tables and then explain what you want as output. thats much easier than analysing query as we dont know how data is present in tables------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
CraigYoungman
Starting Member
6 Posts |
Posted - 2012-05-04 : 08:51:14
|
Here is some sample data.VI_PS_TKT_HIST_LIN -- historical sales history tableItem_no, tkt_dat, qty_sold_stk_unitA , 01/01/12 , 100B , 01/05/12 , 5A , 02/25/12 , 66C , 06/15/12 , 20 A , 08/03/12 , 50IM_ITEM -- Item detail tableitem_no, categ_cod, vendorA , COMPUTER , HPB , PRINTER , HPC , MONITOR , DELLIM_INV -- Inventory detail tableitem_no, loc_id, min_qty, max_qtyA , MAIN , 5 , 12A , EAST , 1 , 5B , MAIN , 1 , 5B , EAST , 1 , 5C , MAIN , 5 , 12 C , EAST , 6 , 15VENDOR_ITEM -- Item detail for vendor tableitem_no, vendor, min_ord_qtyA HP 6A TIGER 1B HP 1C DELL 1The basic formula ismin_qty_to_stock = (lead days + min days supply) * units sold per daymax_qty_to_stock = (lead days + max days supply) * units sold per dayI would also like to account for the minimum order qty for the vendor. Example: you may need 7 but you can only order in lots of 6. So qty ordered would be 6 not 7.Any help would be greatThanks, |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-05 : 01:33:33
|
so as i understand you're trying to get values for min and max qty for items. One question though. I see loc_id also inside table. How do you determine min and max qty based on location? I cant see any rules set based on location in sample------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
CraigYoungman
Starting Member
6 Posts |
Posted - 2012-05-07 : 08:49:49
|
Sorry, I see I left out the loc_id from the VI_PS_TKT_HIST_LIN table. The table should look like this.VI_PS_TKT_HIST_LIN -- historical sales history tableItem_no, tkt_dat, qty_sold_stk_unit, loc_idA , 01/01/12 , 100 , MAINB , 01/05/12 , 5 , MAINA , 02/25/12 , 66 , EASTC , 06/15/12 , 20 , MAINA , 08/03/12 , 50 , EAST |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-07 : 19:57:35
|
Not fully clear still. I cant see the fields you've specified in formula. where are lead days, min days supply etc? Also what should be your output for data above?see how to post proper data for getting quick and accurate solutionshttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
CraigYoungman
Starting Member
6 Posts |
Posted - 2012-05-08 : 08:58:44
|
In my first posting I have two update statements. First I calculate the number of days between two dates with:set @days = datediff(day, @fdate, @tdate) + '1'Then with the first update I set MAX withset max_qty = (select ceiling((@leaddays + @maxdays ) * sum(b.qty_sold_stk_unit) / @days)The second update statement sets the MINset min_qty = (select ceiling((@leaddays + @mindays ) * sum(b.qty_sold_stk_unit) / @days)The output is the update statements. I want it to update data in the inventory table. The goal is to periodically (maybe once or twice a year) edit the script and change the section:-- Set the variables below ---set @fdate = '01/01/2011 00:00:00' -- Starting date in historyset @tdate = '12/31/2011 23:59:59' -- Ending date in historyset @mindays = '5' -- Minimum days of stockset @maxdays = '8' -- Maximum days of stockset @leaddays = '3' -- # of days from placing order to receiving orderset @forloc = 'MAIN' -- For which inventory locationset @vendor = ‘HP’ -- Item vendor (only update items for this vendor)set @cat = 'PRINTER' -- Item category (only update items from this category)When run this will allow the selection of items by specific vendor and category to be updated. When run the script will update the inventory table with new MIN/MAX levels using a sales from the specified period in history.Thanks, |
 |
|
|
|
|
|
|