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:
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.
- 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.
- Align the purchase table and sales table with the date sequence. Two table sequences will be created which are grouped by dates.
- 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.
- Set the primary key of this table as Commodity to facilitate the future searches.
- Write the remaining stocks of last month to table A as the initial stock.
- Loop the date sequence of this month, that is, loop each day.
- In the loop body, retrieve the morning purchase data of this day and add it to table A.
- 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.
- Loop all sales records of the day.
- In the loop body, find out the commodity in table A and count it as stock changes.
- 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.
- 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.
- The accumulation of table A is over at the end of the date loop. Choose the commodity with the longest TotalOosTime.
|5||“08:00:00″||“21:30:00″||Business hours of the supermarket|
=periods@d(date(“2009-6-1″), date(“2009-6-30″), 1)
|12||for A6||=A7(#A12).run(A9.find(Commodity).run(Stock=Stock+Volume))||Loop every day. Summarize commodity purchase volumes in every morning first.|
|14||for A8(#A12)||=A9.find(B14.Commodity)||To make it further, loop every sales record of the day.|
|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.|