我有表名为client_audit,唯一和标识列是recno,clientcode,auditdate,Auditflag,并有其他多个列,这些列往往会被更改,并记录为相同客户代码的审计前和审计后镜像。
auditflag 1表示更改前,auditflag 2表示更改后的映像。
如果我运行select * from client_audit where audit_flag = 1,则记录计数为30000100
如果我运行select * from client_audit where audit_flag = 2,则记录计数为30000000
实际上,对于100条记录,我没有审计后的图像。
现在,记录是成对创建的,就像之前为相同的clientcode创建的一样,并且将创建审计映像,它们的recnumber将按顺序进行。
有没有办法从同一张表中获取这100条记录,它们只有审计镜像之前,没有审计镜像之后,考虑到recno是唯一的,对于相同的客户代码和相同的audidate,身份和审计记录将成对出现
发布于 2019-01-28 13:13:01
如果我正确理解了您的表结构,请尝试以下内容
select * from client_audit where audit_flag = 1 and clientcode not in (select clientcode from client_audit where audit_flag = 2)发布于 2019-01-28 13:32:03
首先,免费提示:如果同时向数据库插入这两条记录,则应使用事务,以便如果其中一次插入失败,则两次插入都会失败,并且您不会像现在那样得到孤立记录。
回答你的问题:
您可以使用not exists根据clientcode和auditdate获取audit_flag = 1中没有与audit_flag = 2对应的记录的所有记录,如下所示:
select *
from client_audit as c1
where audit_flag = 1
and not exists
(
select 1
from client_audit as c2
where audit_flag = 2
and c1.clientcode = c2.clientcode
and c1.auditdate = c2.auditdate
)https://stackoverflow.com/questions/54395807
复制相似问题