繁体   English   中英

SQL查询问题(订票软件)

[英]Issue with SQL Query ( Booking Software )

我有以下小提琴。

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=cd3e6d07208978a987503953b29a1b7e


CREATE TABLE shifts (
    id int NOT NULL,
    unique_id varchar(255) NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE appointments (
    id int NOT NULL,
    unique_id varchar(255) NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    shift_id int NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (shift_id) REFERENCES shifts(id)
);

INSERT INTO `shifts` (`id`, `unique_id`, `start_date`, `end_date`)
VALUES
    ('4596', '614fc0c5dab2485bba8cbbd9eb98aa53', '2022-02-28 10:00:00', '2022-03-15 19:00:00');


INSERT INTO `appointments` (`id`, `unique_id`, `start_date`, `end_date`, `shift_id`)
VALUES
    (18352, 'accdc12943954ee2bd69ad116c2fef3d', '2022-02-28 18:15:00', '2022-02-28 19:00:00', 4596),
    (18351, '6a0d56ad43894b60b4f8a289fbbfff73', '2022-02-28 17:30:00', '2022-02-28 18:15:00', 4596),
    (18545, '4a49c054e59e4514ae2a521b55a7715c', '2022-02-28 16:50:00', '2022-02-28 17:15:00', 4596),
    (18622, '02ca48b35ca3462f9030bcb97ea1dbab', '2022-02-28 16:30:00', '2022-02-28 16:50:00', 4596),
    (18544, 'd27bc7ba34a74f078966ebe1567d3181', '2022-02-28 16:00:00', '2022-02-28 16:30:00', 4596),
    (18685, '4057b87c089b463188a26bc45d85388a', '2022-02-28 15:00:00', '2022-02-28 15:10:00', 4596),
    (18462, '3270929ae6e546fb824e6d3219917b8e', '2022-02-28 14:30:00', '2022-02-28 16:00:00', 4596),
    (18235, 'deac969701ae47f0a78696f12c267475', '2022-02-28 14:00:00', '2022-02-28 14:30:00', 4596),
    (18540, 'e16af8a053594e8b8340ede302398a22', '2022-02-28 13:30:00', '2022-02-28 14:00:00', 4596),
    (18537, 'e1946add03cd412da357b58c93d58c6e', '2022-02-28 12:30:00', '2022-02-28 13:15:00', 4596),
    (18355, 'ec570f9365bb4ad68c7b5ab3b7d9aeea', '2022-02-28 12:00:00', '2022-02-28 12:30:00', 4596),
    (18523, 'aff5abe6e8f84a64ace3277d3cd6dbd1', '2022-02-28 11:00:00', '2022-02-28 12:00:00', 4596),
    (18517, '124baaa1aeab421cb288f2b7c7abe89b', '2022-02-28 10:30:00', '2022-02-28 11:00:00', 4596),
    (18382, '1371e41a7cf342bfa279a8b120cc5f43', '2022-02-28 10:00:00', '2022-02-28 10:30:00', 4596);



SELECT Available_from, Available_to
  FROM (
    SELECT COALESCE(@lasttime_to, '2022-02-28 10:00:00') AS Available_from, start_date AS Available_to, @lasttime_to := end_date
    FROM (SELECT start_date, end_date
             FROM appointments
             WHERE end_date <= '2022-02-28 19:00:00'
             AND start_date >= '2022-02-28 10:00:00'
             UNION ALL (
               SELECT '2022-02-28 19:00:00', '2022-02-28 19:00:00'
             )
             UNION ALL (
               SELECT '2022-02-28 10:00:00', '2022-02-28 10:00:00'
             )
               ORDER BY start_date, end_date
           ) e
    JOIN (SELECT @lasttime_to := NULL) init) x
    WHERE Available_to > DATE_ADD(Available_from, INTERVAL 14 MINUTE);

我怎样才能让这个查询忽略边缘情况,比如那个?

2022-02-28 13:15:00 2022-02-28 13:30:00
2022-02-28 15:10:00 2022-02-28 16:00:00 ( EDGE CASE )
2022-02-28 17:15:00 2022-02-28 17:30:00

这是由持续从

'2022-02-28 15:00:00' TO '2022-02-28 15:10:00'

理想的结果:

2022-02-28 13:15:00 2022-02-28 13:30:00
2022-02-28 17:15:00 2022-02-28 17:30:00

您可以使用相关时间间隔内所有分钟的时间戳记录来查找空闲时间并将它们分组为可用时间间隔。

with t1(c) as (
   select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
), dtally as (
   select  TIMESTAMPADD(minute, row_number() over() - 1, '2022-02-28 10:00:00') t,  row_number() over() rn
   from t1, t1 as t2, t1 as t3, t1 as t4
), freeMins as ( 
   select *, rn - row_number() over(order by rn)  grp
   from dtally
   where t <= '2022-02-28 19:00:00'
   and not exists (
          select 1 
          from appointments a
          where t between a.start_date and a.end_date)
)
select min(t) Available_from, max(t) Available_to
from freeMins
group by grp

退货

Available_from  Available_to
2022-02-28 13:16:00 2022-02-28 13:29:00
2022-02-28 17:16:00 2022-02-28 17:29:00

数据库<>小提琴

暂无
暂无

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

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