T-SQL:条件空删除

T-SQL:条件空删除,sql,sql-server,tsql,null,isnull,Sql,Sql Server,Tsql,Null,Isnull,我只需要选择房间ID,这些ID没有状态为空的实例 例如: 表A Room_Id Status Inspection_Date ----------------------------------- 1 NULL 5/15/2015 2 occupied 5/21/2015 2 NULL 1/19/2016 1 occupied 12/16/2015

我只需要选择
房间ID
,这些ID没有
状态为空的实例

例如:

表A

  Room_Id   Status    Inspection_Date
  -----------------------------------
    1        NULL        5/15/2015
    2        occupied    5/21/2015
    2        NULL        1/19/2016
    1        occupied   12/16/2015
    4        NULL        3/25/2016
    3        vacant      8/27/2015
    1        vacant      4/17/2016
    3        vacant     12/12/2015
    3        vacant      3/22/2016
    4        vacant       2/2/2015
    4        vacant      3/24/2015
我的结果应该如下所示:

  Room_Id  Status  Inspection_Date
  -----------------------------------
    3       vacant      8/27/2015
    3       vacant     12/12/2015
    3       vacant      3/22/2016

因为
Room\u ID
'3'没有
状态为空的实例

您可以使用CTE,而不是像下面的代码那样存在

WITH    bt
      AS ( SELECT   RoomId ,
                    Status,
                    Inspection_Date
           FROM     dbo.Table_1
         )
SELECT  *
FROM    bt AS a
WHERE   NOT EXISTS ( SELECT 1
                     FROM   bt
                     WHERE  bt.RoomId = a.RoomId
                            AND bt.Status IS NULL );

您可以使用CTE,而不是像下面的代码那样存在

WITH    bt
      AS ( SELECT   RoomId ,
                    Status,
                    Inspection_Date
           FROM     dbo.Table_1
         )
SELECT  *
FROM    bt AS a
WHERE   NOT EXISTS ( SELECT 1
                     FROM   bt
                     WHERE  bt.RoomId = a.RoomId
                            AND bt.Status IS NULL );
试试这个:

 SELECT *
 FROM Table1 
 WHERE Room_ID NOT IN 
 (
   SELECT DISTINCT Room_ID
   FROM Table1
   WHERE Status IS NULL
   )
子查询返回一个唯一房间id的列表,这些房间id在某个时候处于空状态。外部查询查看该列表,并显示“Return*,其中room_ID不是子查询中的一个

如果您想在SQL Fiddle中尝试,以下是模式:

CREATE TABLE Table1
    (Room_ID int, Status varchar(8), Inspection_Date datetime)
;

INSERT INTO Table1
    (Room_ID, Status, Inspection_Date)
VALUES
    (1, NULL, '2015-05-15 00:00:00'),
    (2, 'occupied', '2015-05-21 00:00:00'),
    (2, NULL, '2016-01-19 00:00:00'),
    (1, 'occupied', '2015-12-16 00:00:00'),
    (4, NULL, '2016-03-25 00:00:00'),
    (4, 'vacant', '2015-08-27 00:00:00'),
    (1, 'vacant', '2016-04-17 00:00:00'),
    (3, 'vacant', '2015-12-12 00:00:00'),
    (3, 'vacant', '2016-03-22 00:00:00'),
    (4, 'vacant', '2015-02-02 00:00:00'),
    (4, 'vacant', '2015-03-24 00:00:00'),
    (2, NULL, '2015-05-22 00:00:00')
;
试试这个:

 SELECT *
 FROM Table1 
 WHERE Room_ID NOT IN 
 (
   SELECT DISTINCT Room_ID
   FROM Table1
   WHERE Status IS NULL
   )
子查询返回一个唯一房间id的列表,这些房间id在某一时刻处于空状态。外部查询查看该列表,并显示“Return*,其中房间id不是子查询中的房间id”

如果您想在SQL Fiddle中尝试,以下是模式:

CREATE TABLE Table1
    (Room_ID int, Status varchar(8), Inspection_Date datetime)
;

INSERT INTO Table1
    (Room_ID, Status, Inspection_Date)
VALUES
    (1, NULL, '2015-05-15 00:00:00'),
    (2, 'occupied', '2015-05-21 00:00:00'),
    (2, NULL, '2016-01-19 00:00:00'),
    (1, 'occupied', '2015-12-16 00:00:00'),
    (4, NULL, '2016-03-25 00:00:00'),
    (4, 'vacant', '2015-08-27 00:00:00'),
    (1, 'vacant', '2016-04-17 00:00:00'),
    (3, 'vacant', '2015-12-12 00:00:00'),
    (3, 'vacant', '2016-03-22 00:00:00'),
    (4, 'vacant', '2015-02-02 00:00:00'),
    (4, 'vacant', '2015-03-24 00:00:00'),
    (2, NULL, '2015-05-22 00:00:00')
;

如何操作的快速示例:

DECLARE@tTable表(
房间号,
状态VARCHAR(20),
检验(日期时间)
插入到@tTable值中
(1,空,'5/15/2015'),
(1,空,'5/15/2015'),
(2、‘占用’、‘2015年5月21日’,
(2,空,'1/19/2016'),
(1、‘占用’、‘2015年12月16日’,
(4,空,'3/25/2016'),
(3,“空缺”,“2015年8月27日”),
(1,“空缺”,“2016年4月17日”),
(3,“空缺”,“2015年12月12日”),
(3,“空缺”,“2016年3月22日”),
(4,“空缺”,“2015年2月2日”),
(4、‘空缺’、‘2015年3月24日’)
从@tTable T1中选择*
房间Id不在的位置(从@tTable中选择房间Id,其中状态为空)
给出:

Room_Id |   Status |    Inspection_Date         |
-------------------------------------------------
3       |   vacant |    2015-08-27 00:00:00.000
3       |   vacant |    2015-12-12 00:00:00.000
3       |   vacant |    2016-03-22 00:00:00.000

如何操作的快速示例:

DECLARE@tTable表(
房间号,
状态VARCHAR(20),
检验(日期时间)
插入到@tTable值中
(1,空,'5/15/2015'),
(1,空,'5/15/2015'),
(2、‘占用’、‘2015年5月21日’,
(2,空,'1/19/2016'),
(1、‘占用’、‘2015年12月16日’,
(4,空,'3/25/2016'),
(3,“空缺”,“2015年8月27日”),
(1,“空缺”,“2016年4月17日”),
(3,“空缺”,“2015年12月12日”),
(3,“空缺”,“2016年3月22日”),
(4,“空缺”,“2015年2月2日”),
(4、‘空缺’、‘2015年3月24日’)
从@tTable T1中选择*
房间Id不在的位置(从@tTable中选择房间Id,其中状态为空)
给出:

Room_Id |   Status |    Inspection_Date         |
-------------------------------------------------
3       |   vacant |    2015-08-27 00:00:00.000
3       |   vacant |    2015-12-12 00:00:00.000
3       |   vacant |    2016-03-22 00:00:00.000

作为Hashman的替代方法,对于这些类型的查询,我更喜欢使用
notexists
over
notin

创建一些测试数据

请注意,我只是对所有事情都保持相同的日期,因为这不是问题的必要条件

create table #table_a (
    Room_Id int,
    Status varchar(32),
    Inspection_Date date);

insert #table_a (Room_Id, Status, Inspection_Date)
    values
        (1, null, getdate()),
        (2, 'occupied', getdate()),
        (2, null, getdate()),
        (1, 'occupied', getdate()),
        (4, null, getdate()),
        (3, 'vacant', getdate()),
        (1, 'vacant', getdate()),
        (3, 'vacant', getdate()),
        (3, 'vacant', getdate()),
        (4, 'vacant', getdate()),
        (4, 'vacant', getdate());
查询

select *
from #table_a t1
where not exists (
    select *
    from #table_a t2
    where t1.Room_Id = t2.Room_Id
        and Status is null);
结果

Room_Id     Status                           Inspection_Date
----------- -------------------------------- ---------------
3           vacant                           2016-06-17
3           vacant                           2016-06-17
3           vacant                           2016-06-17

作为Hashman的替代方法,对于这些类型的查询,我更喜欢使用
notexists
over
notin

创建一些测试数据

请注意,我只是对所有事情都保持相同的日期,因为这不是问题的必要条件

create table #table_a (
    Room_Id int,
    Status varchar(32),
    Inspection_Date date);

insert #table_a (Room_Id, Status, Inspection_Date)
    values
        (1, null, getdate()),
        (2, 'occupied', getdate()),
        (2, null, getdate()),
        (1, 'occupied', getdate()),
        (4, null, getdate()),
        (3, 'vacant', getdate()),
        (1, 'vacant', getdate()),
        (3, 'vacant', getdate()),
        (3, 'vacant', getdate()),
        (4, 'vacant', getdate()),
        (4, 'vacant', getdate());
查询

select *
from #table_a t1
where not exists (
    select *
    from #table_a t2
    where t1.Room_Id = t2.Room_Id
        and Status is null);
结果

Room_Id     Status                           Inspection_Date
----------- -------------------------------- ---------------
3           vacant                           2016-06-17
3           vacant                           2016-06-17
3           vacant                           2016-06-17

您不需要TSQL来执行此操作。只需使用带有相关子查询的
NOT EXIST
编写一个简单查询。@PM77-1 TSQL是一个查询,TSQL不需要这样做。只需使用带有相关子查询的
NOT EXIST
编写一个简单查询。@PM77-1 TSQL是一个查询这里不需要CTE。@AlexKudryashev别着急,这只是解决方案之一,但它是一个CTE不做任何事情的解决方案。正是这张桌子。你可以参考表格。这里不需要CTE。@阿列克斯库德里亚舍夫别紧张,这只是解决方案之一,但这是一个CTE不起任何作用的解决方案。正是这张桌子。你可以参考表格。