以下是我的时间表表示预留时间从isReserved
列-0未保留。我在这张桌子上有全天可用的时间。SQL Server选择最佳可用时间
我需要从表中选择基于最佳的可用时间。
例子:
如果我需要预订4分钟工作,那么它应该返回第一个可用的时间,当我需要预定30分钟的作业,然后查询不应该返回第一个可作为在该时隙中没有足够的时间可用,并返回下一个可用的时间。
我使用SQL Server 2008 R2的
与jpcbtimeslots以下查询 选择时隙,其中时隙> @starttime和时隙< = @endtime和isreserved = '0'
CREATE TABLE [dbo].[JPCBTimeSlots](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[BayID] [int] NULL,
[TimeSlot] [datetime] NULL,
[IsReserved] [int] NULL,
CONSTRAINT [PK_JPCBTimeSlots] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY])
ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[JPCBTimeSlots] ON
INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (1, 1, CAST(0x0000A62200AC3392 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (2, 1, CAST(0x0000A62200AC79E2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (3, 1, CAST(0x0000A62200ACC032 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (4, 1, CAST(0x0000A62200AD0682 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (5, 1, CAST(0x0000A62200AD4CD2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (6, 1, CAST(0x0000A62200AD9322 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (7, 1, CAST(0x0000A62200ADD972 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (8, 1, CAST(0x0000A62200AE1FC2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (9, 1, CAST(0x0000A62200AE6612 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (10, 1, CAST(0x0000A62200AEAC62 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (11, 1, CAST(0x0000A62200AEF2B2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (12, 1, CAST(0x0000A62200AF3902 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (13, 1, CAST(0x0000A62200AF7F52 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (14, 1, CAST(0x0000A62200AFC5A2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (15, 1, CAST(0x0000A62200B00BF2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (16, 1, CAST(0x0000A62200B05242 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (17, 1, CAST(0x0000A62200B09892 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (18, 1, CAST(0x0000A62200B0DEE2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (19, 1, CAST(0x0000A62200B12532 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (20, 1, CAST(0x0000A62200B16B82 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (21, 1, CAST(0x0000A62200B1B1D2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (22, 1, CAST(0x0000A62200B1F822 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (23, 1, CAST(0x0000A62200B23E72 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (24, 1, CAST(0x0000A62200B284C2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (25, 1, CAST(0x0000A62200B2CB12 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (26, 1, CAST(0x0000A62200B31162 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (27, 1, CAST(0x0000A62200B357B2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (28, 1, CAST(0x0000A62200B39E02 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (29, 1, CAST(0x0000A62200B3E452 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (30, 1, CAST(0x0000A62200B42AA2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (31, 1, CAST(0x0000A62200B470F2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (32, 1, CAST(0x0000A62200B4B742 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (33, 1, CAST(0x0000A62200B4FD92 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (34, 1, CAST(0x0000A62200B543E2 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (35, 1, CAST(0x0000A62200B58A32 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (36, 1, CAST(0x0000A62200B5D082 AS DateTime), 1) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (37, 1, CAST(0x0000A62200B616D2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (38, 1, CAST(0x0000A62200B65D22 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (39, 1, CAST(0x0000A62200B6A372 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (40, 1, CAST(0x0000A62200B6E9C2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (41, 1, CAST(0x0000A62200B73012 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (42, 1, CAST(0x0000A62200B77662 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (43, 1, CAST(0x0000A62200B7BCB2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (44, 1, CAST(0x0000A62200B80302 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (45, 1, CAST(0x0000A62200B84952 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (46, 1, CAST(0x0000A62200B88FA2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (47, 1, CAST(0x0000A62200B8D5F2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (48, 1, CAST(0x0000A62200B91C42 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (49, 1, CAST(0x0000A62200B96292 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (50, 1, CAST(0x0000A62200B9A8E2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (51, 1, CAST(0x0000A62200B9EF32 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (52, 1, CAST(0x0000A62200BA3582 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (53, 1, CAST(0x0000A62200BA7BD2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (54, 1, CAST(0x0000A62200BAC222 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (55, 1, CAST(0x0000A62200BB0872 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (56, 1, CAST(0x0000A62200BB4EC2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (57, 1, CAST(0x0000A62200BB9512 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (58, 1, CAST(0x0000A62200BBDB62 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (59, 1, CAST(0x0000A62200BC21B2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (60, 1, CAST(0x0000A62200BC6802 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (61, 1, CAST(0x0000A62200BCAE52 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (62, 1, CAST(0x0000A62200BCF4A2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (63, 1, CAST(0x0000A62200BD3AF2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (64, 1, CAST(0x0000A62200BD8142 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (65, 1, CAST(0x0000A62200BDC792 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (66, 1, CAST(0x0000A62200BE0DE2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (67, 1, CAST(0x0000A62200BE5432 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (68, 1, CAST(0x0000A62200BE9A82 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (69, 1, CAST(0x0000A62200BEE0D2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (70, 1, CAST(0x0000A62200BF2722 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (71, 1, CAST(0x0000A62200BF6D72 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (72, 1, CAST(0x0000A62200BFB3C2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (73, 1, CAST(0x0000A62200BFFA12 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (74, 1, CAST(0x0000A62200C04062 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (75, 1, CAST(0x0000A62200C086B2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (76, 1, CAST(0x0000A62200C0CD02 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (77, 1, CAST(0x0000A62200C11352 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (78, 1, CAST(0x0000A62200C159A2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (79, 1, CAST(0x0000A62200C19FF2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (80, 1, CAST(0x0000A62200C1E642 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (81, 1, CAST(0x0000A62200C22C92 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (82, 1, CAST(0x0000A62200C272E2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (83, 1, CAST(0x0000A62200C2B932 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (84, 1, CAST(0x0000A62200C2FF82 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (85, 1, CAST(0x0000A62200C345D2 AS DateTime), 0) INSERT [dbo].[JPCBTimeSlots] ([ID], [BayID], [TimeSlot], [IsReserved]) VALUES (50435, 35, CAST(0x0000A62300AC3392 AS DateTime), 0)
SET IDENTITY_INSERT [dbo].[JPCBTimeSlots] OFF /****** Object: Default [DF_JPCBTimeSlots_IsReserved] Script Date: 06/11/2016 17:59:00
******/
ALTER TABLE [dbo].[JPCBTimeSlots]
ADD CONSTRAINT [DF_JPCBTimeSlots_IsReserved] DEFAULT ((0)) FOR [IsReserved]
GO
http://i.stack.imgur.com/EnQ85.png – user2737374
这不是一项免费开发服务。你试过什么查询?还要确定您使用的是哪个数据库。 –
我已经编辑了问题,下面的部分还不清楚“当我需要预订30分钟的工作时,那么查询不应该返回第一个可用的时间戳,因为没有足够的时间可用”..也请标记特定的RDBMS正在使用 – TheGameiswar