Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

php - Calculating total quantity of equipments for a date range

Project: I am working on a project which is about some rooms and equipments using in the rooms. The software is about scheduling the equipments in the rooms. In other words, it is a reservation software that reserves selected equipments in separate rooms for needed dates and times ranges. I have many tables in MYsSQL database working with Php but I will mention the tables my question is about. The tables I will relate my questions are equipment table (Table A), schedule table (Table B) and equipments using in the related schedule (Table C).

Table A: equipment list table

eqid   | eqName    | available| 

1      |  book     |   90     |      

2      |  pen      |   82     | 

3      |  computer |   25     |

In table A; eqid represents unique id of an equipment, eqName represents name of an equipment, available represents total available equipments existing.

Table B: schedule table

scheduleid | startDate  | endDate    | startTime | endTime  |  office  |

1          | 2012-08-27 | 2012-08-27 | 08:30:00  | 10:00:00 |  room1   |

2          | 2012-08-27 | 2012-08-27 | 09:30:00  | 11:00:00 |  room3   |

3          | 2012-08-28 | 2012-08-30 | 08:30:00  | 12:00:00 |  room2   |

4          | 2012-08-29 | 2012-08-31 | 11:30:00  | 14:00:00 |  room1   |

5          | 2012-08-28 | 2012-08-28 | 10:30:00  | 14:00:00 |  room3   |

6          | 2012-08-27 | 2012-08-30 | 08:30:00  | 10:00:00 |  room4   |

7          | 2012-08-27 | 2012-08-27 | 10:30:00  | 12:00:00 |  room4   |    

8          | 2012-08-27 | 2012-08-30 | 08:30:00  | 11:00:00 |  room6   |

9          | 2012-08-27 | 2012-08-27 | 10:30:00  | 12:00:00 |  room5   |

In table B; scheduleid represents unique id for a schedule, startDate and endDate are date range for a schedule, startTime and endTime time range for a schedule, office means that where the schedule will take place. Let me give an example here. Scheduleid 1 means there is a reservation on 27th of august 2012, Monday and it is from 08.30 to 10:00. As it start and end on same day this is just one day reservation in room1. However, Scheduleid 3 means there is a reservation starts on 28th of august 2012, Tuesday and goes on until 30th of august 2012, Thursday at 08:30-12:00... in other words, it lasts for 3 days and everyday from 08:30 to 12:00... So there is a reservation from Tuesday to Thursday at 08:30 to 12:00 in room2... I hope this is clear.

Table C: equipments using in the related schedule

Autoid  | scheduleid |  eqid   | amountInSch|

1       |     1      |   1     |      2     |      

2       |     1      |   2     |      3     |  

3       |     1      |   3     |      1     |  

4       |     2      |   1     |      1     |  

5       |     2      |   2     |      1     |  

6       |     2      |   3     |      2     |  

7       |     3      |   2     |      1     | 

8       |     3      |   3     |      3     |  

9       |     4      |   2     |      1     |  

10      |     4      |   3     |      1     |  

11      |     5      |   1     |      1     |  

12      |     6      |   1     |      1     | 

13      |     6      |   3     |      2     |  

14      |     6      |   2     |      4     |  

15      |     7      |   1     |      5     |  

16      |     7      |   2     |      6     |  

17      |     8      |   2     |      1     | 

18      |     9      |   1     |      8     |  

19      |     9      |   2     |      5     |  

20      |     9      |   3     |      6     |  

In table C: Autoid represents unique automatic id generated by auto-increment, scheduleid comes from Table B, eqid comes from Table A, amountInSch represents how many (amount) equipment will use in the related schedule. I want to give an example here. Scheduleid 1 in Table C, there are 3 rows. This means that scheduleid 1 related in TAble B will use 2 books (eqid 1), 3 pens (eqid 2) and 1 computer (eqid 3) in room1 specified dates and times in table B . Another example is that scheduleid 3 in Table C is related 2 rows. It means that 1 pen (eqId 2) and 3 computers (eqId 3) will be using in room2 from 27th to 30th of august 2012 everyday from 08:30 to 12:00.

The above is the explanation and give some information about the project. The table rows are not permanent. When you make a reservation, there will be a new row in Table B and if it is selected an equipment, there will be new rows in table C...

The Question:

I want to calculate left amount of a specific equipment when I supply eqId, startDate, endDate, startTime and endTime...

An example:

eqId: 1 (book)

startDate: 2012-08-27

endDate: 2012-08-27

startTime: 08:30:00

endTime: 12:00:00

Result should be: 14 books used in schedule and 76 left available books

Because: if you look scheduleIds and related eqIds, you will only see 1, 2, 6, 7, 9 scheduleIds related to my query(dates and eqId). If you sum the all amount of related in Table C, you will get the wrong result. In other words, related amounts for eqId(1-book) and for 1, 2, 6, 7, 9 scheduleIds are 2, 1, 1, 5, 8 respectively. So if you sum them you will get 17 which is wrong. Because, 1 and 9 schedule don't intersect each other in terms of start and end Times, and 6 and 7 don't intersect each other either. as a result of them 2 stays lonely and can be count separately. We must consider 1 and 9 as summed 8 because 8 is bigger than 2. it is same for 6 and 7, considered as 5 because of 5 is bigger than 1...

So folks! I am not sure how I can sum/ this in programming algorithm. Is there a way to do in SQL or do I have to use PHP and Mysql together? and How?

Cheers!

SQLFiddle Records

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I started with the following SQL to gather all date ranges that intersect with the given range:

SELECT MAX(available) - IFNULL(SUM(amountInSch), 0)
FROM Table1
LEFT JOIN Table3 USING (eqid)
LEFT JOIN Table2 USING (scheduleid)
WHERE DATE(startDate) <= '2012-08-27' AND DATE(endDate) >= '2012-08-27'
  AND endTime > '08:30' AND startTime < '12:00'
  AND eqid = 1

Fiddle

This is only the first part. Next up you have to work out the possible overlaps; this wouldn't be practical to do with SQL, so I would suggest to do this in PHP.

The generic algorithm I would pick is unfortunately O(n**2), it goes like this:

  • create a timeline (demarcated by each day) with time as the horizontal axis
  • iterate over every date/time range and mark the time of its left and right edge to create time segments of every possible permutation.
  • using the segments, you sum vertically for overlaps and you take the daily maximum across.

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...