尝试查找每天两次时间戳之间预订资源的总时间-假设08:00-16:00。
到目前为止,我想出了这个解决方案,但是它没有按预期工作。我知道为什么它不起作用,但是我不确定如何从这里过渡到有效的SQL。
SELECT bookingresource.resourceID, SEC_TO_TIME(SUM(TIME_TO_SEC(booking.bookingEndDatetime) - TIME_TO_SEC(booking.bookingStartDatetime))) AS totalTimeBooked
FROM booking, bookingresource
WHERE booking.bookingID = bookingresource.bookingID
AND bookingresource.bookingStartDatetime BETWEEN '2019-03-11 00:00:00' AND '2019-03-11 23:59:59'
AND HOUR(bookingresource.bookingStartDatetime) BETWEEN 8 AND 16
AND HOUR(bookingresource.bookingEndDatetime) BETWEEN 8 AND 16
AND booking.companyID = 2447 AND bookingresource.`resourceID` = 807
GROUP BY bookingresource.resourceID
通过这种方式,我不包括不希望在8-16岁开始或结束的预订。如何包含部分预订时间在08:00-16:00之间的预订,并且仅计算08:00-16:00内的预订时间。
编辑:我将尝试简化SQL,因此没有太多不必要的代码。现在我的问题是总时间的计算方式。目前,如果预订部分在时间范围内,那么它将包括时间范围之外的时间。
SELECT bookingresource.resourceID, SEC_TO_TIME(SUM(TIME_TO_SEC(booking.bookingEndDatetime) - TIME_TO_SEC(booking.bookingStartDatetime))) AS timediff FROM booking, bookingresource
WHERE booking.bookingID = bookingresource.bookingID
AND (HOUR(bookingresource.bookingStartDatetime) BETWEEN 8 AND 16
OR HOUR(bookingresource.bookingEndDatetime) BETWEEN 8 AND 16)
GROUP BY bookingresource.resourceID
编辑2:所以我找到了一个解决方案,但预订超过一天的订单却遇到了问题。到目前为止,这是我的解决方案:
SELECT bookingresource.resourceID, SEC_TO_TIME(sum(LEAST(TIME_TO_SEC(TIME(booking.bookingEndDatetime)), TIME_TO_SEC('16:00')) - GREATEST(TIME_TO_SEC(TIME(booking.bookingStartDatetime)), TIME_TO_SEC('08:00')))) AS totalTimeBooked FROM booking, bookingresource
WHERE booking.bookingID = bookingresource.bookingID
AND bookingresource.bookingStartDatetime BETWEEN '2019-04-10 00:00:00' AND '2019-04-10 23:59:59'
AND (TIME(bookingresource.bookingStartDatetime) BETWEEN '08:00' AND '16:00'
OR TIME(bookingresource.bookingEndDatetime) BETWEEN '08:00' AND '16:00')
AND bookingresource.`resourceID` = 453
group by bookingresource.resourceID
答案 0 :(得分:0)
Try convert you string date in proper datetime value
and if you don't want values outside the range 8 - 16 then you should also check that the date for star and end is the same
SELECT bookingresource.resourceID
, SEC_TO_TIME(SUM(TIME_TO_SEC(booking.bookingEndDatetime) - TIME_TO_SEC(booking.bookingStartDatetime))) AS totalTimeBooked
FROM booking
INNER JOIN bookingresource ON booking.bookingID = bookingresource.bookingID
WHERE bookingresource.bookingStartDatetime BETWEEN str_to_date('2019-03-11 00:80:00', '%Y-%m-%d %T')
AND str_to_date('2019-03-11 23:59:59', '%Y-%m-%d %T')
AND HOUR(bookingresource.bookingStartDatetime) BETWEEN 8 AND 16
AND HOUR(bookingresource.bookingEndDatetime) BETWEEN 8 AND 16
AND DATE(bookingresource.bookingStartDatetime) = DATE(bookingresource.bookingEndDatetime)
AND booking.companyID = 2447
AND bookingresource.`resourceID` = 807
GROUP BY bookingresource.resourceID