php – 查询:根据日期计算平均住宿价格

大家好,

我需要为房屋搜索创建一个查询,该查询将匹配数据库用户输入的数据:他们想要进入和离开的日期,他们在组中的人数和每晚的价格.

让我们说用户搜索了房子:

日期:2011-01-15至2011-03-01(见图片期A1C1),3人,他愿意每晚花费90至125美元.

这是我对此搜索的手动计算:

>数据库中可用的日期
>用户想要入住的日期总数为:44天
> 2011-01-15至2011-01-25第一期的价格是10天* 100美元= 1000美元
>第二期201​​1-01-25至2011-02-14的价格是20天* 120美元= 2400美元
>第二期201​​1-02-14至2011-03-01的价格是14天* 140美元= 1960美元
>每晚总平均价格= 1000 2400 1960/44 = $121.8
>价格和人数匹配用户输入,所以我们显示这个房子

如果合并日期并计算给定期间的每晚平均价格,则搜索脚本应与上面提供的数据数组匹配.

我的问题是:如果用户数据与数据库中的记录匹配,我的查询应如何快速计算.

我正在考虑使用SQL DATEDIFF函数,然后乘以价格等等,但它看起来相当复杂.

我将不胜感激任何建议.

谢谢

UPDATE

这是我的数据库架构:

表“apt_search_periods”,用于存储所有合并日期(可用性表中的连续日期)

+-----------+------------+------------+-----------+--------------+--------+
| period_id | start_date | end_date   | rental_id | nb_of_people | merged |
+-----------+------------+------------+-----------+--------------+--------+
|        21 | 2011-03-31 | 2012-03-31 |       548 |            4 | y      |
+-----------+------------+------------+-----------+--------------+--------+

表“apt_search_periods_avail”将合并日期与可用性表相关联

+----+-----------+-----------------+
| id | period_id | availability_id |
+----+-----------+-----------------+
| 21 |        21 |           20953 |
| 22 |        21 |           20952 |
| 23 |        21 |            4033 |
+----+-----------+-----------------+

表“可用性”,包含扩展日期和价格

+-------+-----------+------------+------------+--------------+--------------+
| 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 |
+-------+-----------+------------+------------+--------------+--------------+

最佳答案 以下应该让你开始.

请注意,唯一的区别是根据DATEDIFF,第三个时期包括15天.

SQL语句

;WITH q AS (
  /* Kick of with the record where startdate < input < enddate */
  SELECT  date_start
          , date_end
  FROM    @HouseSearch
  WHERE   date_start <= @date_start
          AND date_end >= @date_start
          AND nb_people >= @nb_people -- Only when number of people is adequate
  UNION ALL
  SELECT  q.date_start
          , hs.date_end
  FROM    q
          INNER JOIN @HouseSearch hs ON hs.date_start = q.date_end
  WHERE   nb_people >= @nb_people -- Only when number of people is adequate
)
SELECT  *
FROM    (
          -- Only return result if sequence exists between date range
          SELECT  date_start = MIN(date_start)
                  , date_end = MAX(date_end)
          FROM    q
          WHERE   date_end >= @date_end 
        ) datetimerange
        -- Calculate the average price
        CROSS APPLY (
          SELECT  [AveragePrice] = SUM(price / DATEDIFF(dd, @date_start, @date_end))
          FROM    (
                    -- Price for all records where date_end <= @date_end 
                    SELECT  [price] = 
                              CASE WHEN @date_start < date_start 
                              THEN DATEDIFF(dd, date_start, date_end) * price
                              ELSE DATEDIFF(dd, @date_start, date_end) * price
                              END                        
                    FROM    @HouseSearch        
                    WHERE   @date_end > date_end
                    UNION ALL
                    -- Price of remaining records where date_end >= @date_end
                    SELECT  DATEDIFF(dd, date_start, @date_end) * price
                    FROM    @HouseSearch        
                    WHERE   @date_end between date_start AND date_end
                  ) prices
        ) price                  
WHERE   date_start IS NOT NULL            

测试数据

DECLARE @HouseSearch TABLE (
  date_start DATE
  , date_end DATE
  , nb_people INTEGER
  , price FLOAT
)  

INSERT INTO @HouseSearch VALUES
  ('2011-01-01', '2011-01-25', 4, 100)
  , ('2011-01-25', '2011-02-14', 3, 120)
  , ('2011-02-14', '2011-03-12', 3, 140)
  , ('2011-03-12', '2011-04-10', 3, 100)

DECLARE @date_start DATE = '2011-01-15'
DECLARE @date_end DATE = '2011-03-01'
DECLARE @nb_people INTEGER = 3
DECLARE @price_low FLOAT = 90
DECLARE @price_high FLOAT = 15
点赞