SQL:删除范围

时间:2017-09-29 11:49:07

标签: sql sql-server

如何删除END TIME(第3列)和下一个开始时间(第2列)之间的所有NULL值(时间间隔)?

谢谢!

表示例:

NULL    06:00:00.0000000    NULL
NULL    06:15:00.0000000    NULL
NULL    06:30:00.0000000    NULL
NULL    06:45:00.0000000    NULL
NULL    07:00:00.0000000    NULL
NULL    07:15:00.0000000    NULL
NULL    07:30:00.0000000    NULL
        07:30:00.0000000    08:30:00.0000000
NULL    07:45:00.0000000    NULL
NULL    08:00:00.0000000    NULL
NULL    08:15:00.0000000    NULL
NULL    08:30:00.0000000    NULL
        08:30:00.0000000    09:00:00.0000000
NULL    08:45:00.0000000    NULL
NULL    09:00:00.0000000    NULL
        09:00:00.0000000    09:30:00.0000000
NULL    09:15:00.0000000    NULL
NULL    09:30:00.0000000    NULL
        09:30:00.0000000    09:45:00.0000000

3 个答案:

答案 0 :(得分:0)

您似乎只想保留最初的NULL值。所以,像:

select t.*
from t
where t.endtime is not null or
      t.endtime < (select min(t2.endtime) from t t2);

答案 1 :(得分:0)

请查看本文如何获取sql server中的上一行,然后检查prev值和当前值是否为null: https://blog.sqlauthority.com/2013/09/22/sql-server-how-to-access-the-previous-row-and-next-row-value-in-select-statement/

答案 2 :(得分:-1)

 DELETE FROM table WHERE id BETWEEN 79 AND 296