简体   繁体   English

平均日期的sql特性

[英]sql charateristic function for avg dates

I have a query which I use to grab specific dates and a price for the date, but now I'd like to use something similar to grab the avg prices for particular days of the week. 我有一个查询,用于获取特定日期和该日期的价格,但现在我想使用类似的方法来获取一周中特定日期的平均价格。

Here's my current query which works for specific dates to pull from a table called availables: 这是我当前的查询,适用于特定日期以从称为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

My first stab which doesn't work, probably because the DAYSOFWEEK function is much different from the to_days... 我的第一个选项无效,可能是因为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

Maybe i'm making this crazy hard and someone knows a much simpler way. 也许我正在加倍努力,有人知道一种更简单的方法。

It takes data that looks like this 这需要看起来像这样的数据

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

to: 至:

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

If I understand correctly, it looks like you just need to take out " - DAYOFWEEK('2009-12-10')" since DAYOFWEEK(availables.bookdate) is already returning you a number representing the day of the week. 如果我的理解正确,则您似乎只需要删除“-DAYOFWEEK('2009-12-10')”,因为DAYOFWEEK(availables.bookdate)已经向您返回了代表星期几的数字。

Also, DAYOFWEEK returns a number 1 through 7, not 0 through 6, so you would need to adjust accordingly. 另外,DAYOFWEEK返回的数字是1到7,而不是0到6,因此您需要进行相应的调整。

It may be more efficient to do a "GROUP BY room_id, DAYOFWEEK(availables.bookdate)" to group the average prices by room and day in an inner subquery, and then do the pivoting in an outer query. 在内部子查询中执行“按room_id,DAYOFWEEK(availables.bookdate)分组”以按房间和天的平均价格分组,然后在外部查询中进行透视可能会更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM