查询以选择用户开始/结束日期时间落在日期时间范围内

时间:2015-04-08 02:45:05

标签: sql sql-server sql-server-2008

我有两个表ResourceAssign存储员工工作班。另一个表是存储项目处理日期时间。下面是创建表和示例数据的脚本。

GO

/ ******对象:表[dbo]。[ResourceAssign]脚本日期:04/07/2015 09:05:35 ****** /

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ResourceAssign](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ResourceID] [nvarchar](50) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[Inactive] [int] NULL,
CONSTRAINT [PK_ResourceAssign] 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


INSERT INTO [ResourceAssign]
([ResourceID],[StartDate],[EndDate],[Inactive])VALUES
('I0109', '2015-03-30 08:30:00.000',    '2015-03-30 16:30:00.000', 0),
('I0104',   '2015-03-30 08:30:00.000',  '2015-03-30 16:30:00.000', 0),
('I0108',   '2015-03-30 08:30:00.000',  '2015-03-30 16:30:00.000', 0),
('I0109',   '2015-03-30 16:30:00.000',  '2015-03-31 00:30:00.000', 0),
('I0107',   '2015-03-30 16:30:00.000',  '2015-03-31 00:30:00.000', 0),
('I0108',   '2015-03-30 16:30:00.000',  '2015-03-31 00:30:00.000', 0),
('I0109',   '2015-03-31 08:30:00.000',  '2015-03-31 16:30:00.000', 0),
('I0108',   '2015-03-31 16:30:00.000',  '2015-04-01 00:30:00.000', 0),
('I0107',   '2015-03-31 16:30:00.000',  '2015-04-01 00:30:00.000', 0),
('I0109',   '2015-04-01 00:30:00.000',  '2015-04-01 08:30:00.000', 0),
('I0104',   '2015-04-01 08:30:00.000',  '2015-04-01 16:30:00.000', 0),
('I0107',   '2015-04-01 08:30:00.000',  '2015-04-01 16:30:00.000', 0),
('I0108',   '2015-04-01 16:30:00.000',  '2015-04-02 00:30:00.000', 0),
('I0104',   '2015-04-02 00:30:00.000',  '2015-04-02 08:30:00.000', 0),
('I0107',   '2015-04-02 08:30:00.000',  '2015-04-02 16:30:00.000', 0),
('I0109',   '2015-04-02 08:30:00.000',  '2015-04-02 16:30:00.000', 0),
('I0104',   '2015-04-03 00:30:00.000',  '2015-04-03 08:30:00.000', 0),
('I0107',   '2015-04-03 00:30:00.000',  '2015-04-03 08:30:00.000', 0),
('I0108',   '2015-04-03 08:30:00.000',  '2015-04-03 16:30:00.000', 0),
('I0109',   '2015-04-03 16:30:00.000',  '2015-04-04 00:30:00.000', 0)
GO


CREATE TABLE [dbo].[QuantityProcess](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Length1] [decimal](18, 2) NOT NULL,
[Length2] [decimal](18, 2) NOT NULL,
[LengthMeterSqr] [decimal](18, 4) NOT NULL,
[TotalLength] [decimal](18, 2) NOT NULL,
[StartProcessTime] [datetime] NULL,
[EndProcessTime] [datetime] NULL,
[DurationUse] [nchar](10) NULL,
CONSTRAINT [PK_QuantityProcess] 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


INSERT INTO [inno_Edging].[dbo].[QuantityProcess]
([Length1],[Length2],[LengthMeterSqr],[TotalLength],[StartProcessTime]
,[EndProcessTime]
,[DurationUse])VALUES
(223.00,    219.00, 0.0488, 884.00, '2015-04-01 16:30:19.000',  '2015-04-  
 01 16:30:52.000',  33), 
(224.00,    294.00, 0.0659, 1036.00, '2015-04-01 16:31:08.000', '2015-04-  
01 16:32:06.000',   58), 
(226.00,    224.00, 0.0506, 900.00, '2015-04-01 16:35:36.000',  '2015-04-
01 16:36:05.000',   29), 
(223.00,    221.00, 0.0493, 888.00, '2015-04-01 16:32:16.000',  '2015-04-
01 16:34:21.000',   125),
(228.00,    228.00, 0.0520, 912.00, '2015-04-01 18:35:36.000',  '2015-04-
01 18:37:36.000',   120), 
(223.00,    219.00, 0.0488, 884.00, '2015-04-01 18:38:36.000',  '2015-04-
01 18:40:36.000',   120), 
(228.00,    228.00, 0.0520, 912.00, '2015-04-02 08:58:49.000',  '2015-04-
02 08:59:35.000',   46), 
(230.00,    229.00, 0.0527, 918.00, '2015-04-02 08:59:43.000',  '2015-04-
02 09:04:09.000',   266), 
(226.00,    227.00, 0.0513, 906.00, '2015-04-02 09:04:19.000',  '2015-04-
02 09:05:37.000',   78), 
(223.00,    219.00, 0.0488, 884.00, '2015-04-02 00:10:36.000',  '2015-04-
02 00:11:36.000',   60) , 
(223.00,    219.00, 0.0488, 884.00, '2015-04-02 00:13:36.000',  '2015-04- 
02 00:15:36.000',   120),
(223.00,    223.00, 0.0497, 892.00, '2015-04-02 09:57:39.000',  '2015-04-
02 09:57:46.000',   7), 
(233.00,    233.00, 0.0543, 932.00, '2015-04-02 12:59:27.000',  '2015-04-
02 13:00:18.000',   51), 
(229.00,    232.00, 0.0531, 922.00, '2015-04-02 13:02:02.000',  '2015-04-
02 13:02:55.000',   53), 
(216.00,    217.00, 0.0469, 866.00, '2015-04-03 11:15:08.000',  '2015-04-
03 11:15:45.000',   37), 
(326.00,    220.00, 0.0717, 1092.00,    '2015-04-03 11:15:53.000',   
'2015-04-03 11:16:13.000',  20), 
(258.00,    329.00, 0.0849, 1174.00,    '2015-04-03 11:16:25.000',  
'2015-04-03 11:16:40.000',  15), 
(233.00,    214.00, 0.0499, 894.00, '2015-04-03 12:15:20.000',  '2015-04-
03 12:15:30.000',   10), 
(321.00,    229.00, 0.0735, 1100.00,    '2015-04-06 15:09:20.000',  
'2015-04-06 15:09:27.000',  7)

在我的系统中,用户允许选择开始日期时间和结束日期时间以获得结果。用户可以从系统中选择开始日期2015-04-01 16:31:00和结束日期2015-04-01 19:30:00,以找出在此日期时间范围内处理的项目。使用相同的日期时间范围,用户需要知道分配谁来处理此项目。资源(员工)根据班次(每班8小时)进行分配。因此,如果用户选择开始日期2015-04-01 16:31:00和结束日期2015-04-01 19:30:00来查询项目流程,他们还需要知道谁在那个负责期间来处理选择时间范围的项目。如何获得日期时间用户选择的结果是在哪个班次?

预期的结果是

数量处理表

(224.00,    294.00, 0.0659, 1036.00, '2015-04-01 16:31:08.000', '2015-04-
 01 16:32:06.000',  58), 
(226.00,    224.00, 0.0506, 900.00, '2015-04-01 16:35:36.000',  '2015-04-
01 16:36:05.000',   29), 
(223.00,    221.00, 0.0493, 888.00, '2015-04-01 16:32:16.000',  '2015-04- 
01 16:34:21.000',   125),
(228.00,    228.00, 0.0520, 912.00, '2015-04-01 18:35:36.000',  '2015-04-
01 18:37:36.000',   120), 
(223.00,    219.00, 0.0488, 884.00, '2015-04-01 18:38:36.000',  '2015-04-
01 18:40:36.000',   120), 

资源分配表

(' I0108',' 2015-04-01 16:30:00.000',' 2015-04-02 00:30:00.000' ,0)

另一种情况是,如果用户选择日期范围是开始日期2015-04-01 00:00:01和结束日期2015-04-01 23:59:59,结果预期如下。即使只有1-4-2015 16:30:19 - 1-4-2015 18:40:36有项目处理,但是在用户选择的时间范围内有很少的班次或员工。它还需要作为资源分配表的结果进行查询。

数量处理表

(223.00,    219.00, 0.0488, 884.00, '2015-04-01 16:30:19.000',  '2015-04-
01 16:30:52.000',   33), 
(224.00,    294.00, 0.0659, 1036.00, '2015-04-01 16:31:08.000', '2015-04-
01 16:32:06.000',   58), 
(226.00,    224.00, 0.0506, 900.00, '2015-04-01 16:35:36.000',  '2015-04-
01 16:36:05.000',   29), 
(223.00,    221.00, 0.0493, 888.00, '2015-04-01 16:32:16.000',  '2015-04-
01 16:34:21.000',   125),
(228.00,    228.00, 0.0520, 912.00, '2015-04-01 18:35:36.000',  '2015-04-
01 18:37:36.000',   120), 
(223.00,    219.00, 0.0488, 884.00, '2015-04-01 18:38:36.000',  '2015-04-
01 18:40:36.000',   120), 

资源分配表

('I0109',   '2015-04-01 00:30:00.000',  '2015-04-01 08:30:00.000', 0),
('I0104',   '2015-04-01 08:30:00.000',  '2015-04-01 16:30:00.000', 0),
('I0107',   '2015-04-01 08:30:00.000',  '2015-04-01 16:30:00.000', 0),
('I0108',   '2015-04-01 16:30:00.000',  '2015-04-02 00:30:00.000', 0),

知道我怎么能得到这个结果吗?

1 个答案:

答案 0 :(得分:0)

您可以获取资源满足以下其中一项的Resources列表

  1. 在开始和结束范围内的整个转变
  2. 班次在范围之前开始,在范围开始之后结束
  3. 在范围开始后开始转换,在范围结束后结束

    DECLARE @Startdate DATETIME = '2015-04-01 16:31:00'
    DECLARE @Enddate DATETIME = '2015-04-01 19:30:00'
    --SELECT @Startdate  = '2015-04-01 00:00:01',@Enddate = '2015-04-01 23:59:59'
    SELECT * FROM ResourceAssign
        WHERE (StartDate <= @Startdate AND EndDate >= @Enddate)
        OR (StartDate <=@Startdate AND Enddate >= @Startdate )
        OR (StartDate <=@Enddate AND EndDate >=@Enddate)
    
    SELECT * FROM QuantityProcess
    WHERE (StartProcessTime >=@Startdate AND EndProcessTime <= @Enddate)
    
  4. 您可以取消注释SELECT以获取第二个示例的输出。