sql特征函数的平均日期

时间:2009-10-20 13:14:13

标签: sql ruby-on-rails pivot pivot-table

我有一个查询,我用它来获取日期的具体日期和价格,但现在我想使用类似的东西来获取一周中特定日期的平均价格。

这是我当前的查询,它适用于从名为availables的表中提取的特定日期:

SELECT rooms.name, rooms.roomtype, rooms.id, max(availables.updated_at),
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 0, (availables.price*0.66795805223432), '')) AS day1,
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 1, (availables.price*0.66795805223432), '')) AS day2,
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 2, (availables.price*0.66795805223432), '')) AS day3,
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 3, (availables.price*0.66795805223432), '')) AS day4,
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 4, (availables.price*0.66795805223432), '')) AS day5,
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 5, (availables.price*0.66795805223432), '')) AS day6,
MAX(IF(to_days(availables.bookdate) - to_days('2009-12-10') = 6, (availables.price*0.66795805223432), '')) AS day7,
MIN(spots) as spots
     FROM `availables`
     INNER JOIN rooms
     ON availables.room_id=rooms.id
     WHERE rooms.hotel_id = '5064' AND bookdate
     BETWEEN '2009-12-10' AND DATE_ADD('2009-12-10', INTERVAL 6 DAY)
     GROUP BY rooms.name
     ORDER BY rooms.ppl

我的第一次尝试不起作用,可能是因为DAYSOFWEEK功能与to_days有很大不同......

SELECT rooms.id, rooms.name,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 0, (availables.price*0.66795805223432), '')) AS day1,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 1, (availables.price*0.66795805223432), '')) AS day2,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 2, (availables.price*0.66795805223432), '')) AS day3,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 3, (availables.price*0.66795805223432), '')) AS day4,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 4, (availables.price*0.66795805223432), '')) AS day5,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 5, (availables.price*0.66795805223432), '')) AS day6,
MAX(IF(DAYOFWEEK(availables.bookdate) - DAYOFWEEK('2009-12-10') = 6, (availables.price*0.66795805223432), '')) AS day7,rooms.ppl AS spots FROM `availables` 
 INNER JOIN `rooms` ON `rooms`.id = `availables`.room_id 
 WHERE (rooms.hotel_id = 5064 AND rooms.ppl > 3 AND availables.price > 0 AND availables.spots > 1) 
 GROUP BY rooms.name
 ORDER BY rooms.ppl

也许我正在疯狂地努力,有人知道一种更为简单的方式。

它需要看起来像这样的数据

#Availables
id    room_id   price    spots    bookdate
1     26        $5       5        2009-10-20
2     26        $6       5        2009-10-21

为:

+----+-------+--------------------+---------------------+---------------------+---------------------+------+------+------+------+
| id | spots | name               | day1                | day2                | day3                | day4 | day5 | day6 | day7 |
+----+-------+--------------------+---------------------+---------------------+---------------------+------+------+------+------+
| 25 | 4     | Blue Room          | 14.9889786921381408 | 14.9889786921381408 | 14.9889786921381408 |      |      |      |      |
| 26 | 6     | Whatever           | 13.7398971344599624 | 13.7398971344599624 | 13.7398971344599624 |      |      |      |      |
| 27 | 8     | Some name          | 11.2417340191036056 | 11.2417340191036056 | 11.2417340191036056 |      |      |      |      |
| 28 | 8     | Another            | 9.9926524614254272  | 9.9926524614254272  | 9.9926524614254272  |      |      |      |      |
| 29 | 10    | Stuff              | 7.4944893460690704  | 7.4944893460690704  | 7.4944893460690704  |      |      |      |      |
+----+-------+--------------------+---------------------+---------------------+---------------------+------+------+------+---

1 个答案:

答案 0 :(得分:0)

如果我理解正确,看起来你只需要取出“ - DAYOFWEEK('2009-12-10')”因为DAYOFWEEK(availables.bookdate)已经返回一个代表星期几的数字。

此外,DAYOFWEEK返回1到7的数字,而不是0到6,所以你需要相应地调整。

执行“GROUP BY room_id,DAYOFWEEK(availables.bookdate)”以在内部子查询中按房间和日期对平均价格进行分组可能更有效,然后在外部查询中进行透视。