计算给定可用性和现有预约的开放时间段 - 按天

发布于 2024-10-09 10:41:46 字数 503 浏览 0 评论 0原文

概述:

我有一个表,存储一个人当天的“可用性”,例如

周一 - 上午 8:00 - 上午 11:30
周一 - 下午 1:30 - 下午 6:00

第二个表存储此人当天已有的约会,例如

周一 - 上午 8:30 - 上午 11:00
周一 - 下午 2:30 - 下午 4 点

期望结果:
进行计算我希望得到以下结果 - 例如“此人在指定日期有空”:

周一 - 上午 8:00 - 上午 8:30
周一 - 上午 11:00 - 上午 11:30
周一 - 下午 1:30 - 下午 2:30
周一 - 下午 4:00 - 下午 6:00

如果有任何有关如何计算给定两个输入(例如可用性、现有预约)的输出的想法,我们将不胜感激。最好我在客户端上使用 javascript 来进行计算,因为我相信在数据库中进行计算(我使用 MSSQL)对于许多记录、人员等来说会很慢。

希望这是足够的信息来说明问题手头 - 非常感谢。

Overview:

I have a table which stores a persons "availability" for a current day, e.g.

Mon - 8:00am - 11:30am
Mon - 1:30pm - 6:00pm

A second table stores appointments that this person already has for the same day, e.g.

Mon - 8:30am - 11:00am
Mon - 2:30pm - 4pm

Desired result:
Doing calculationsI'd like to have the following result - e.g. "this person has availability on the given day":

Mon - 8:00am - 8:30am
Mon - 11:00am - 11:30am
Mon - 1:30pm - 2:30pm
Mon - 4:00pm - 6:00pm

Any ideas on how to calculate the output given the two inputs (e.g. availability, existing appointments) would be greatly appreciated. Preferably I'd use javascript on the client to do the calculating as I would believe that doing it within the DB (I'm using MSSQL) would be slow for many records, persons, etc.

Hope this is enough information to illustrate the problem at hand - Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2024-10-16 10:41:46

你需要离散化你的时间。选择一个时间间隔用作原子。根据您的示例,这可能应该是半小时。

现在

Create table Availability (person_id int, interval_id int);
Create table Appointment (person_id int, interval_id int, appointment_desc text);

我省略了主键,并且应该有外键来查找 Person 和 Interval 的表。

将有一个 Interval 表用于查找每个 Interval_id 代表什么。

Create table Interval(interval_id int primary key, interval_start datetime, interval_end datetime)

将日历中的每个间隔填充到间隔表中。填充它可能是一件苦差事,但您可以在 Excel 中创建实际值,然后将它们粘贴到间隔表中。

现在您可以找到空闲间隔,因为

Select person_id, interval_id from Availability av
left join Appointment ap
on av.person_id = ap.person_id and av.interval_id = ap.interval_id
where ap.interval_id is null

MSSQL 可以立即执行这种外连接(前提是您设置了键),并且您可以在发送的页面中包含空闲间隔列表,并使用 javascript 来显示它们:想要的。

You need to discretize your time. Choose a time interval to use as your atom. Based on your example, that should probably be a half hour.

Now

Create table Availability (person_id int, interval_id int);
Create table Appointment (person_id int, interval_id int, appointment_desc text);

I'm leaving out the primary keys, and there should be foreign keys to lookup tables for Person and Interval.

There will be an Interval table for looking up what each interval_id stands for.

Create table Interval(interval_id int primary key, interval_start datetime, interval_end datetime)

Populate the Interval table with every interval you're going to have in your calendar. Populating it might be a chore, but you can create the actual values in Excel, then paste them into your Interval table.

Now you can find free intervals as

Select person_id, interval_id from Availability av
left join Appointment ap
on av.person_id = ap.person_id and av.interval_id = ap.interval_id
where ap.interval_id is null

MSSQL can do this kind of outer join in no time (provided you set up the keys), and you can include the list of free intervals in the pages you send, with javascript to display them when and as desired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文