如果存在一次记录,则过滤掉相关记录

时间:2018-01-24 22:11:10

标签: sql db2

我有下表(不包括所有数据):

Company   Account   Acct_Unit   System
10        10510     10000000    GL
10        10510     10000000    ML
10        10990     10000020    ML
10        25310     10000000    AP
10        25310     10000000    BR

我正在尝试删除所有帐户字符串记录(公司/帐户/ Acct_Unit),其中一条记录= ML。

以下是删除后我的表格的样子:

Company   Account   Acct_Unit   System
10        25310     10000000    AP
10        25310     10000000    BR

我有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

不存在有效。 Alternate是一个左连接,遵循以下逻辑:取表并左键将其连接到自身的子选择,其中system =' GL'。如果找到ml记录,则将填充左连接表帐户字段,因此查找左连接记录为空的记录。

select a.* from following_table a
left join (select account,Acct_Unit from following_table where System = 'GL') b
on a.account = b.account and a.acct_unit = b.acct_unit
where b.account is null

出于某种原因,我离开了公司。如果需要,请随意添加。

相关问题