SQL“Where In”表示空子查询

时间:2016-03-29 02:09:31

标签: sql-server sql-server-2012 where-in

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

数据:

gid         time                name
1010883478  29/03/2016 0:00:02  John
1010883527  29/03/2016 0:00:04  John
1010883578  29/03/2016 0:00:06  John

SQL:

  SELECT A.[gid]
       ,A.[time]
       ,A.[name]
       ,(B.[time] - A.[time]) as timeTilNext
  FROM [location] A CROSS JOIN [location] B 
  WHERE B.[gid] IN (
      SELECT MIN(C.[gid]) 
      FROM [location] C 
      WHERE C.[gid] > A.[gid] AND C.[name] = A.[name] )
  ORDER BY A.[gid]

当前输出:

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)

但是,它没有显示给定[名称]的最高[gid]的记录(仅次于第二高)。

我希望最高[gid]为timeTilNext显示-1,表示没有更多事件。

有关如何修改查询的任何想法?

3 个答案:

答案 0 :(得分:2)

在SQL Server 2012中,您可以使用LEAD窗口函数来获取" next"的值。行。

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 |
+------------+-------------------------+------+-------------------------+-----------------+

如果&#34;下一个&#34;行无法使用,LEAD将返回NULL。如果需要,可以使用ISNULL()将其替换为某些非空值。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

SELECT A.gid,A.name,A.time, (     (SELECT MIN(B.time)FROM [location] B WHERE B.time&gt; A.time AND B.name = A.name)      -     A.time )作为timeTilNext

FROM [location] A