【发布时间】:2019-09-18 00:32:10
【问题描述】:
我有一张叫做交流的桌子。该表包含少量带有contact_id 的记录和少量不带有contact_id 的记录。因此,应该将包含contact_id 的comm_type 和ident 列与不包含contact_id 的comm_type 和ident 列进行比较。所以,如果不为空的contact_id的comm_type和ident与null的contact_id的comm_type和ident匹配,那么null的contact comm_type和ident应该被剔除
id contact_id comm_type ident
109901; 114351; 3 "1111111111";
97631; 102177; 2 "Konnection hub#12403";
102924; 109096; 3 "1111111111";
id contact_id comm_type ident
109901; 3 "1111111111";
97631; 2 "Konnection hub#12403";
102924; 4 "Aptech interval";
在这种情况下,应该删除不包含contact_id的前两条记录,因为它的comm_type和ident与包含contact_id的记录匹配。
我已经尝试过这个查询,但这并没有让我得到正确的输出:-
BEGIN;
delete from crm.comm_medium m1 where contact_id is not null and exists
(select 1 from crm.comm_medium m2 where m2.comm_type_id =m1.comm_type_id and m2.ident=m1.ident and contact_id is null)
【问题讨论】:
标签: postgresql