Sql server SQL";“在哪里?”;对于空子查询

Sql server SQL";“在哪里?”;对于空子查询,sql-server,sql-server-2012,where-in,Sql Server,Sql Server 2012,Where In,我有下面的查询,其目的是显示每条记录到下一条记录的时间 数据: SQL: 电流输出: gid time name timeTilNext 1010883478 2016-03-29 00:00:02.000 John 1900-01-01 00:00:02.000 1010883527 2016-03-29 00:00:04.000 John 1900-01-01 00:00:02.000 预期产出: gid

我有下面的查询,其目的是显示每条记录到下一条记录的时间

数据:

SQL:

电流输出:

gid         time                    name    timeTilNext
1010883478  2016-03-29 00:00:02.000 John    1900-01-01 00:00:02.000
1010883527  2016-03-29 00:00:04.000 John    1900-01-01 00:00:02.000
预期产出:

gid         time                    name    timeTilNext
1010883478  2016-03-29 00:00:02.000 John    1900-01-01 00:00:02.000
1010883527  2016-03-29 00:00:04.000 John    1900-01-01 00:00:02.000
1010883578  2016-03-29 00:00:06.000 John    -1 (or whatever)
但是,它不会显示给定[name]的最高[gid]记录(仅显示第二高的记录)

我希望最高的[gid]在timeTilNext中显示-1,以表明没有更多的事件


关于如何修改我的查询有什么想法吗?

在SQL Server 2012中,您可以使用窗口函数获取“下一行”的值

select 
*,-1 as 'time until next ' from location t1
where time=(select max(time) from location t2 where t1.name=t2.name) b
结果

+------------+-------------------------+------+-------------------------+-----------------+
|    gid     |          time           | name |        NextTime         | SecondsTillNext |
+------------+-------------------------+------+-------------------------+-----------------+
| 1010883478 | 2016-03-29 00:00:02.000 | John | 2016-03-29 00:00:04.000 |               2 |
| 1010883527 | 2016-03-29 00:00:04.000 | John | 2016-03-29 00:00:06.000 |               2 |
| 1010883578 | 2016-03-29 00:00:06.000 | John | NULL                    |              -1 |
+------------+-------------------------+------+-------------------------+-----------------+

如果“下一行”不可用,
LEAD
将返回
NULL
。如果需要,可以使用
ISNULL()
将其替换为一些非空值。

选择A.gid、A.name、A.time、, ( (从[location]B中选择MIN(B.time),其中B.time>A.time,B.name=A.name) - A.时间 )直到下一次


请从[location]A

发布一些样本数据和预期结果。您使用的是什么版本的SQL Server?不确定为什么拒绝投票以及为什么删除答案?@Zeophlite,您的问题的
expected
部分令人困惑。如果我理解正确,您的
输出
部分应标记为
当前输出
,并按现在的状态显示两行。
expected
部分应准确显示您期望的内容,即所有三行,而不仅仅是一行。也许这种混乱是投票失败的原因。@VladimirBaranov,说得好,我已经更新了问题,以反映您是否可以更新此内容以匹配问题中的标题?这与您问题的标题匹配,唯一的更改是tablename,我现在已经更改了它
select 
*,-1 as 'time until next ' from location t1
where time=(select max(time) from location t2 where t1.name=t2.name) b
DECLARE @location TABLE ([gid] int, [time] datetime, [name] varchar(50));

INSERT INTO @location ([gid], [time], [name]) VALUES
(1010883478, '2016-03-29 00:00:02', 'John'),
(1010883527, '2016-03-29 00:00:04', 'John'),
(1010883578, '2016-03-29 00:00:06', 'John');

SELECT 
    A.[gid]
    ,A.[time]
    ,A.[name]
    ,LEAD(A.[time]) OVER(PARTITION BY A.[name] ORDER BY A.[gid]) AS NextTime
    ,ISNULL(DATEDIFF(second, A.[time], 
        LEAD(A.[time]) OVER(PARTITION BY A.[name] ORDER BY A.[gid])), -1) AS SecondsTillNext
FROM @location A
ORDER BY A.[gid];
+------------+-------------------------+------+-------------------------+-----------------+
|    gid     |          time           | name |        NextTime         | SecondsTillNext |
+------------+-------------------------+------+-------------------------+-----------------+
| 1010883478 | 2016-03-29 00:00:02.000 | John | 2016-03-29 00:00:04.000 |               2 |
| 1010883527 | 2016-03-29 00:00:04.000 | John | 2016-03-29 00:00:06.000 |               2 |
| 1010883578 | 2016-03-29 00:00:06.000 | John | NULL                    |              -1 |
+------------+-------------------------+------+-------------------------+-----------------+