MS SQL查询返回下一个开始时间和上一个结束时间

时间:2014-01-24 16:43:03

标签: sql sql-server time sql-server-2012

我有一个包含列,Machine,job_name,Start_time的表。机器和作业名称是文本,Start_time是日期/时间。

我想要一个查询,它将返回机器的下一个按时间顺序的开始时间作为结束时间。

我尝试过使用MIN(start_time)>开始时间。但是,这会为每个可能的结束时间返回一行多个开始时间。我不确定如何将每行的Start_time识别为比较start_time。

表示例。

MAchine, Job_name, Start_time


scanner, A1A, 1/24/2014 8:00am

scanner, ABA, 1/24/2014 12:00pm

scanner, A1B, 1/24/2014 10:00pm

scanner, AC3, 1/25/2014 4:00am

scanner, AG2, 1/25/2014 11:00am

scanner, BK4, 1/25/2014 3:00pm

scanner1, AA, 1/24/2014 9:00am

scanner1, AA, 1/24/2014 1:00pm

scanner1, AB, 1/24/2014 8:00pm

scanner1, A3, 1/25/2014 2:00am

scanner1, A2, 1/25/2014 7:00am

scanner1, B4, 1/25/2014 2:00pm

scanner2, A1, 1/24/2014 11:00am

scanner2, AB, 1/24/2014 12:00pm

scanner2, A1, 1/24/2014 5:00pm

scanner2, AC, 1/25/2014 1:00am

scanner2, A2, 1/25/2014 5:00am

scanner2, K4, 1/25/2014 2:00pm

我想在下面找到结果。

MAchine, Job_name, Start_time

scanner, A1A, 1/24/2014 8:00am, 1/24/2014 12:00pm

scanner, ABA, 1/24/2014 12:00pm, 1/24/2014 10:00pm

scanner, A1B, 1/24/2014 10:00pm, 1/25/2014 4:00am

scanner, AC3, 1/25/2014 4:00am, 1/25/2014 11:00am

scanner, AG2, 1/25/2014 11:00am, 1/25/201 3:00pm

scanner, BK4, 1/25/2014 3:00pm, 

scanner1, AA, 1/24/2014 9:00am, 1/24/2014 1:00pm

scanner1, AA, 1/24/2014 1:00pm, 1/24/2014 8:00pm

scanner1, AB, 1/24/2014 8:00pm, 1/25/2014 2:00am

scanner1, A3, 1/25/2014 2:00am, 1/25/2014 7:00am

scanner1, A2, 1/25/2014 7:00am, 1/25/2014 2:00pm

scanner1, B4, 1/25/2014 2:00pm, 

scanner2, A1, 1/24/2014 11:00am, 1/24/2014 12:00pm

scanner2, AB, 1/24/2014 12:00pm, 1/24/2014 5:00pm

scanner2, A1, 1/24/2014 5:00pm, 1/25/2014 1:00am

scanner2, AC, 1/25/2014 1:00am, 1/25/2014 5:00am

scanner2, A2, 1/25/2014 5:00am, 1/25/2014 2:00pm

scanner2, K4, 1/25/2014 2:00pm, 

2 个答案:

答案 0 :(得分:1)

您需要考虑查看SQL Server 2012及更高版本中可用的LAGLEAD函数。

答案 1 :(得分:0)

这种方法适用于大多数数据库:

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Table1
    ([MAchine] varchar(8), [Job_name] varchar(3), [Start_time] datetime)
;

INSERT INTO Table1
    ([MAchine], [Job_name], [Start_time])
VALUES
    ('scanner', 'A1A', '1/24/2014 8:00am'),
    ('scanner', 'ABA', '1/24/2014 12:00pm'),
    ('scanner', 'A1B', '1/24/2014 10:00pm'),
    ('scanner', 'AC3', '1/25/2014 4:00am'),
    ('scanner', 'AG2', '1/25/2014 11:00am'),
    ('scanner', 'BK4', '1/25/2014 3:00pm'),
    ('scanner1', 'AA', '1/24/2014 9:00am'),
    ('scanner1', 'AA', '1/24/2014 1:00pm'),
    ('scanner1', 'AB', '1/24/2014 8:00pm'),
    ('scanner1', 'A3', '1/25/2014 2:00am'),
    ('scanner1', 'A2', '1/25/2014 7:00am'),
    ('scanner1', 'B4', '1/25/2014 2:00pm'),
    ('scanner2', 'A1', '1/24/2014 11:00am'),
    ('scanner2', 'AB', '1/24/2014 12:00pm'),
    ('scanner2', 'A1', '1/24/2014 5:00pm'),
    ('scanner2', 'AC', '1/25/2014 1:00am'),
    ('scanner2', 'A2', '1/25/2014 5:00am'),
    ('scanner2', 'K4', '1/25/2014 2:00pm')
;

查询1

select t3.MAchine, t3.Job_name, t3.Start_time, tm.MinNextTime
from Table1 t3
left outer join (
 select t1.MAchine, t1.Start_time, min(t2.Start_time) as MinNextTime
 from Table1 t1
 inner join Table1 t2 on t1.MAchine = t2.MAchine and t2.Start_time > t1.Start_time
 group by t1.MAchine, t1.Start_time
) tm on t3.MAchine = tm.MAchine and t3.Start_time = tm.Start_time
order by t3.MAchine, t3.Start_time

<强> Results

|  MACHINE | JOB_NAME |                     START_TIME |                    MINNEXTTIME |
|----------|----------|--------------------------------|--------------------------------|
|  scanner |      A1A | January, 24 2014 08:00:00+0000 | January, 24 2014 12:00:00+0000 |
|  scanner |      ABA | January, 24 2014 12:00:00+0000 | January, 24 2014 22:00:00+0000 |
|  scanner |      A1B | January, 24 2014 22:00:00+0000 | January, 25 2014 04:00:00+0000 |
|  scanner |      AC3 | January, 25 2014 04:00:00+0000 | January, 25 2014 11:00:00+0000 |
|  scanner |      AG2 | January, 25 2014 11:00:00+0000 | January, 25 2014 15:00:00+0000 |
|  scanner |      BK4 | January, 25 2014 15:00:00+0000 |                         (null) |
| scanner1 |       AA | January, 24 2014 09:00:00+0000 | January, 24 2014 13:00:00+0000 |
| scanner1 |       AA | January, 24 2014 13:00:00+0000 | January, 24 2014 20:00:00+0000 |
| scanner1 |       AB | January, 24 2014 20:00:00+0000 | January, 25 2014 02:00:00+0000 |
| scanner1 |       A3 | January, 25 2014 02:00:00+0000 | January, 25 2014 07:00:00+0000 |
| scanner1 |       A2 | January, 25 2014 07:00:00+0000 | January, 25 2014 14:00:00+0000 |
| scanner1 |       B4 | January, 25 2014 14:00:00+0000 |                         (null) |
| scanner2 |       A1 | January, 24 2014 11:00:00+0000 | January, 24 2014 12:00:00+0000 |
| scanner2 |       AB | January, 24 2014 12:00:00+0000 | January, 24 2014 17:00:00+0000 |
| scanner2 |       A1 | January, 24 2014 17:00:00+0000 | January, 25 2014 01:00:00+0000 |
| scanner2 |       AC | January, 25 2014 01:00:00+0000 | January, 25 2014 05:00:00+0000 |
| scanner2 |       A2 | January, 25 2014 05:00:00+0000 | January, 25 2014 14:00:00+0000 |
| scanner2 |       K4 | January, 25 2014 14:00:00+0000 |                         (null) |