如果表中存在其他数据,则根据条件从表中删除行

时间:2016-03-11 12:13:32

标签: sql-server tsql

我有一个查询返回的以下数据: -

CUSTACCOUNT     DIVISION        EXTPERSON    SALESMAN
C0001729            ECD        5637263283    Ian
C0001729            Fuel       5637369057  Peter
C0001729            Fuel       NULL        House
C0001729            ECD        NULL          House
C0001729            BSC        5637263239    Andrew

我想要一种方法来删除EXTPERSON列中包含null的所有行,只有当另一行具有相同的DIVISION并且EXTPERSON具有值时,如果它们没有那么留下行而不删除...我希望这是有道理的。有没有办法做到这一点?

有一种简单的方法吗?

6 个答案:

答案 0 :(得分:0)

您可以使用以下查询:

DELETE FROM t 
FROM tbl t 
WHERE 
 EXTPERSON IS NULL 
  AND EXISTS( 
   SELECT 1 FROM tbl WHERE t.DIVISION =DIVISION AND EXTPERSON IS NOT NULL 
   )

说明:

我们正在删除null的行,并在外部WHERE查询部分中检查其他具有dame DIVISION但在EXTPERSON中具有值的行。

测试脚本:

create table tbl ( CUSTACCOUNT  nvarchar(20),DIVISION  nvarchar(20), EXTPERSON  nvarchar(20),SALESMAN nvarchar(20))
INSERT INTO tbl values
 ('C0001729','ECD','5637263283','Ian')
,('C0001729','Fuel','5637369057','Peter')
,('C0001729','Fuel',       NULL,'House')
,('C0001729','ECD',        NULL,'House')
,('C0001729','BSC','5637263239','Andrew')

select * from tbl
begin transaction

DELETE FROM t 
FROM tbl t 
WHERE 
 EXTPERSON IS NULL 
  AND EXISTS( 
   SELECT 1 FROM tbl WHERE t.DIVISION =DIVISION AND EXTPERSON IS NOT NULL 
   )

select * from tbl
rollback transaction

drop table tbl

<强>结果:

enter image description here

答案 1 :(得分:0)

对于这样的任务,我曾经离开过加入并检查我在哪里有链接。

例如,对于SQL结构:

`a`(`id`, `type`, `val`)

如果我想“仅选择包含ID的行,那些已经具有相同类型的ID”......

select a1.* from `a` a1 left join `a` a2 on a2.`type`=a1.`type` and a2.`id`!=a1.`id` where a2.`id` is not null;

这样的任务也可以使用group by,count,具有子查询来执行......但子查询是。呃。子查询。

答案 2 :(得分:0)

请使用以下代码,假设#ACCOUNTS有您的表格数据

DELETE A FROM #ACCOUNTS A
WHERE EXTPERSON   IS NULL
AND EXISTS (SELECT 1 FROM #ACCOUNTS B
WHERE A.DIVISION = B.DIVISION   
AND ISNULL(B.EXTPERSON,0)<>0)

答案 3 :(得分:0)

DELETE from TABLE1 T1 where EXISTS
(select CUSTACCOUNT, DIVISION, EXTPERSON,SALESMAN  from TABLE2  T2

where EXTPERSON IS NULL AND  T1.DIVISION<>T2.DIVISION
) and EXTPERSON   IS NULL

答案 4 :(得分:0)

示例数据设置:

create table #data (CUSTACCOUNT varchar(50), DIVISION varchar(50), EXTPERSON varchar(50), SALESMAN varchar(50))
insert into #data values
    ('C0001729', 'ECD', '5637263283', 'Ian'),
    ('C0001729', 'Fuel', '5637369057', 'Peter'),
    ('C0001729', 'Fuel', NULL, 'House'),
    ('C0001729', 'ECD', NULL, 'House'),
    ('C0001729', 'BSC', '5637263239', 'Andrew'),
    ('C0001729', 'SomeOther', NULL, 'Name')

删除查询:

;with aData
as (
    select
        rn = row_number() over (partition by DIVISION order by EXTPERSON desc),
        cntExt = count(EXTPERSON) over (partition by DIVISION)
    from #data
)
delete from aData
where rn > cntExt and cntExt > 0

检查表格数据:

select * from #data

输出:

CUSTACCOUNT  DIVISION    EXTPERSON   SALESMAN
------------ ----------- ----------- ----------
C0001729     ECD         5637263283  Ian
C0001729     Fuel        5637369057  Peter
C0001729     BSC         5637263239  Andrew
C0001729     SomeOther   NULL        Name

答案 5 :(得分:0)

PictureBox