mySQL两个日期之间的平均差异

时间:2015-08-27 14:31:55

标签: mysql sql inner-join min datediff

有人可以帮我解决这个问题吗?我正在查看accommodation_offer.created_at和booking.requested_at,了解第二个结果的第一个和最小结果之间的差异。

我在'字段列表'错误

中收到一个未知列'minbooking'

我已经创建了minbooking,因为我最初认为有一个嵌套选择会有助于AVG(DATEDIFF(MIN)(我最初的查询。从那以后我删除了嵌套的Select并且一直尝试这种方式但没有成功

顺便说一句,优惠可以有多个预订。我希望按城市分组结果,然后按优惠创建日期,然后显示创建的优惠和首次预订每个优惠之间的平均时差。

SELECT
accommodation_offer.created_at_month AS "Month of Offer Created",
property.address_city_code AS "City",
MIN(booking.requested_at) AS minbooking,
AVG(DATEDIFF(minbooking,accommodation_offer.created_at)) AS "Time Span in     Days"
FROM booking
INNER JOIN accommodation_offer
ON accommodation_offer.id=booking.offer_id
INNER JOIN property
ON accommodation_offer.property_id=property.id
GROUP BY property.address_city_code, accommodation_offer.created_at_month
ORDER BY property.address_city_code, accommodation_offer.created_at_month ASC

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT
accommodation_offer.created_at_month AS "Month of Offer Created",
property.address_city_code AS "City",
MIN(booking.requested_at) AS minbooking,
AVG(DATEDIFF(MIN(booking.requested_at),accommodation_offer.created_at_month)) AS  
"Time Span in     Days"
FROM booking
INNER JOIN accommodation_offer
ON accommodation_offer.id=booking.offer_id
INNER JOIN property
ON accommodation_offer.property_id=property.id
GROUP BY property.address_city_code, accommodation_offer.created_at_month
ORDER BY property.address_city_code, accommodation_offer.created_at_month 
ASC