﻿

Commodities with Longest Accumulated Out-of-Stock Time in a Month

Problem

The tables below are from a simplified supermarket stock control system. To check if the purchase policy is proper, the supermarket needs to find out the commodity with the longest accumulated out-of-stock time in 6 months. The supermarket opens at 8 a.m. and closes at 9:30 p.m. The out-of-stock time of non-business hours will not be counted. The desired table is as follows:

The supermarket replenishes its stock at 5 a.m. every day as shown in the below purchase table which records the volume of every piece of commodity.

The table below is about the remaining stock table by the last day of May:

The table below is a detailed sales record of the supermarket:

Tip

Rough train of thought: Create a table to record the remaining stocks and the out-of-stock commodities first. Group the purchase table and sales table by date and loop every day. Traverse every purchase and sales record to get statistics of out-of-stock commodities and accumulated time of out-of-stock commodities. Then, the total out-of-stock time and the commodity with longest accumulated out-of-stock time can be found out.

1. Create a table sequence to record all dates of the month and align it with the purchase and sales tables. This is to ensure that the purchase table and sales table are aligned completely, so as to avoid the mistake that no commodities had been replenished is shown one day.
2. Align the purchase table and sales table with the date sequence. Two table sequences will be created which are grouped by dates.
3. Create a table sequence A to record the remaining stocks and the out-of-stock commodities. Table sequence A must comprise Commodity, Stock, OosTime, and TotalOosTime fields.
4. Set the primary key of this table as Commodity to facilitate the future searches.
5. Write the remaining stocks of last month to table A as the initial stock.
6. Loop the date sequence of this month, that is, loop each day.
7. In the loop body, retrieve the morning purchase data of this day and add it to table A.
8. Find out the commodities whose stock is 0 in table A. The OosTime is the opening time of the supermarket, that is, 8 a.m.
9. Loop all sales records of the day.
10. In the loop body, find out the commodity in table A and count it as stock changes.
11. Check if the Stock value turns to be 0. If so, then it is out of stock on that day. Write down the OosTime as current time in table A.
12. At the end of the loop, select all out-of-stock commodities of the day in table A. The closing time of the supermarket is the end time. Work out the out-of-stock time point and add it to the field of TotalOosTime in table A.
13. The accumulation of table A is over at the end of the date loop. Choose the commodity with the longest TotalOosTime.

Code

 A B C D 1 =file(“C:\\txt\\Stock.txt”).import@t().select(month(Datetime)==6) 2 =file(“C:\\txt\\Sale.txt”).import@t().select(month(Datetime)==6) 3 =file(“C:\\txt\\Storage.txt”).import@t().select(month(Date)==5) 4 =file(“C:\\txt\\Commodity.txt”).import@t() 5 “08:00:00″ “21:30:00″ Business hours of the supermarket 6 =periods@d(date(“2009-6-1″), date(“2009-6-30″), 1) 7 =A1.align(A6:~,date(Datetime)) 8 =A2.align(A6:~,date(Datetime)) 9 =A4.new(ID:Commodity,0:Stock,:OosTime,0:TotalOosTime) 10 >A9.primary@b(Commodity) 11 =A3.run(A9.find(Commodity).Stock=Stock) 12 for A6 =A7(#A12).run(A9.find(Commodity).run(Stock=Stock+Volume)) Loop every day. Summarize commodity purchase volumes in every morning first. 13 =A9.select(Stock<=0).run(OosTime=string(A12)+” “+A5) 14 for A8(#A12) =A9.find(B14.Commodity) To make it further, loop every sales record of the day. 15 >C14.run(Stock=Stock-B14.Volume) 16 if C14.Stock<=0 >C14.OosTime=B14.Datetime If the commodities run out of stock after transactions, write down the current time as the start point of out-of-stock. 17 =A9.select(Stock<=0) 18 >B17.run(TotalOosTime=TotalOosTime+interval@s(OosTime,string(A12)+” “+C5)) 19 =A9.maxp(TotalOosTime).Commodity

﻿