
Hello all,
I need to create a query for house search, that would match in database user entered data: date when they want to move in and leave, number of people they have in group and price per night.
Lets say user searched for house:
dates: from 2011-01-15 to 2011-03-01 (see on picture period A1C1), for 3 people, and he is willing to spend from $90 to $125 dollars per night.
This is my manual calculations for this search:
- dates available in database
- total number of dates user wants to stay is: 44 days
- price for the first period 2011-01-15 to 2011-01-25 is 10 days * $100 = $1000
- price for the second period 2011-01-25 to 2011-02-14 is 20 days * $120 = $2400
- price for the third period 2011-02-14 to 2011-03-01 is 14 days * $140 = $1960
- total average price per night = 1000 + 2400 + 1960 / 44 = $121.8
- price and number of people matches user input, so we display this house
If you merge dates and calculate average price per night for the given period, search script should match array of data provided above.
My question is this: How my query should look like to calculate quickly if user data matches records in database.
I was thinking about using SQL DATEDIFF function and then multiply by price ... etc but it looks to me pretty complex.
I will appreciate any advice.
Thank you
UPDATE
Here is my database schema:
Table "apt_search_periods" which stores all merged dates (continuous dates from availability table)
+-----------+------------+------------+-----------+--------------+--------+
| period_id | start_date | end_date | rental_id | nb_of_people | merged |
+-----------+------------+------------+-----------+--------------+--------+
| 21 | 2011-03-31 | 2012-03-31 | 548 | 4 | y |
+-----------+------------+------------+-----------+--------------+--------+
Table "apt_search_periods_avail" linking merged dates with availability table
+----+-----------+-----------------+
| id | period_id | availability_id |
+----+-----------+-----------------+
| 21 | 21 | 20953 |
| 22 | 21 | 20952 |
| 23 | 21 | 4033 |
+----+-----------+-----------------+
Table "availability" with expanded dates and prices
+-------+-----------+------------+------------+--------------+--------------+
| id | rental_id | start_date | end_date | nb_of_people | rent_per_day |
+-------+-----------+------------+------------+--------------+--------------+
| 20952 | 548 | 2011-03-31 | 2011-07-01 | 4 | 575 |
| 4033 | 548 | 2011-07-01 | 2011-09-01 | 4 | 680 |
| 20953 | 548 | 2011-09-01 | 2012-03-31 | 4 | 575 |
+-------+-----------+------------+------------+--------------+--------------+