从两个不同日期之间的mysql查询中获取最大和最小时数

时间:2015-01-20 13:45:48

标签: mysql sql

一些示例数据......

booking_date booking_time bookingstart_time     bookingend_time
2015-01-12                2015-01-12 18:25:00   2015-01-12 20:45:00
2015-01-12                2015-01-12 18:30:00   2015-01-12 20:30:00
2015-01-12                2015-01-12 20:30:00   2015-01-12 22:00:00
2015-01-12                2015-01-12 19:00:00   2015-01-12 20:20:00
2015-01-12                2015-01-12 20:30:00   2015-01-12 22:25:00
2015-01-12                2015-01-12 20:00:00   2015-01-12 21:40:00
2015-01-12                2015-01-12 19:00:00   2015-01-12 21:20:00
2015-01-13                2015-01-13 14:00:00   2015-01-13 16:10:00
2015-01-13                2015-01-13 13:30:00   2015-01-13 16:00:00
2015-01-13                2015-01-13 17:00:00   2015-01-13 19:05:00
2015-01-13                2015-01-13 18:05:00   2015-01-13 19:50:00
2015-01-16                2015-01-16 19:30:00   2015-01-16 21:20:00
2015-01-18                2015-01-18 12:30:00   2015-01-18 14:35:00
2015-01-18                2015-01-18 15:00:00   2015-01-18 16:50:00
2015-01-18                2015-01-18 12:30:00   2015-01-18 14:00:00
2015-01-18                2015-01-18 14:30:00   2015-01-18 15:35:00
2015-01-18                2015-01-18 12:30:00   2015-01-18 13:55:00
2015-01-18                2015-01-18 16:00:00   2015-01-18 17:40:00
2015-01-18                2015-01-18 14:30:00   2015-01-18 16:05:00

我希望从两个不同日期之间的bookingstart_time获得最小和最长时间。例如:2015-01-06至2015-01-18,

我的查询是,

SELECT `booking_date`, MAX(HOUR(bookingstart_time)) 
AS highdemand,MIN(HOUR(bookingstart_time)) 
AS lowdemand FROM (`rest_restaurantbooking`) 
WHERE `res_id` = '17' AND `booking_date` >= '2015-01-06' 
AND `booking_date` <= '2015-01-18'.

但我在同一天获得MAX和MIN输出。 喜欢

enter image description here

这是假的。

我需要输出一些, enter image description here 这两个值都在同一个日期。它应该在不同的日期。  我在这里犯了什么错误。

2 个答案:

答案 0 :(得分:1)

你有booking_date字段让事情变得简单。只需按该字段分组,即可获得每个日期的最早和最新条目:

select  booking_date, Min( bookingstart_time ) Earliest, Max( bookingend_time ) Latest
from    rest_restaurantbooking;

现在您有一个包含第一个和最后一个预订时间的日期列表。将其提供给查询,该查询隔离时间部分并选择单行结果集的所有日期的第一次和最后一次预订。然后将输出连接在一起以选择特定日期。

with
Q1 as(
    select  booking_date,
            Min( bookingstart_time ) as Earliest,
            Max( bookingend_time ) as Latest
    from    @Bookings
    group by booking_date
),
Q2 as(
    select  Min( Cast( Earliest as time )) as Earliest, Max( cast( Latest as time )) as Latest
    from    Q1
)
select  case when Q2.Latest = Cast( Q1.Latest as time )
            then 'Latest Demand'
            else 'Earliest Demand' end as Demand,
        Q1.booking_date as BookingDate,
        case when Q2.Latest = Cast( Q1.Latest as time ) then Q1.Latest
            else Q1.Earliest end as BookingTime
from    Q1
join    Q2
    on  Q2.Earliest = Cast( Q1.Earliest as time )
    or  Q2.Latest   = Cast( Q1.Latest as time );

返回此内容:

Demand          BookingDate  BookingTime
--------------- ------------ -----------------------
Latest Demand   2015-01-12   2015-01-12 22:25:00.000
Earliest Demand 2015-01-18   2015-01-18 12:30:00.000

是的,我知道,MySQL没有CTE。所以here是使用表格的小提琴。您可以在代码中创建和填充临时表。

答案 1 :(得分:0)

更新第3个

试试这个......

SELECT 'Highest Demand' As type
     , booking_date
     , Max(Hour(bookingstart_time)) As demand_time
FROM   rest_restaurantbooking
WHERE  res_id = '17'
AND    booking_date >= '2015-01-06'
AND    booking_date <= '2015-01-18'
GROUP
    BY booking_date

UNION ALL

SELECT 'Lowest Demand' As type
     , booking_date
     , Min(Hour(bookingstart_time)) As demand_time
FROM   rest_restaurantbooking
WHERE  res_id = '17'
AND    booking_date >= '2015-01-06'
AND    booking_date <= '2015-01-18'
GROUP
    BY booking_date