如何筛选miembro_documento重复且关联证书日期相同的有效记录
优化后的SQL查询方案
目标查询语句
SELECT pm.miembro_documento, p.postulante_postulacion_fecha, COUNT(DISTINCT pm.miembro_postulante) AS 重复计数 FROM PostulantesMiembros pm JOIN Postulantes p ON pm.miembro_postulante = p.postulante_id GROUP BY pm.miembro_documento, p.postulante_postulacion_fecha HAVING COUNT(DISTINCT pm.miembro_postulante) > 1;
关键优化点说明
- 用
COUNT(DISTINCT pm.miembro_postulante)替代普通COUNT:确保只统计不同证书ID的数量,直接排除同一证书ID下重复出现相同证件号的无效记录 - 分组维度同时包含
miembro_documento和postulante_postulacion_fecha:精准筛选出「同一证件号+同一证书分配日期」下对应多个不同证书ID的场景 - 通过JOIN关联两张表,一次性获取证件号、证书日期及关联证书ID的关联关系,避免多次子查询带来的性能损耗
内容的提问来源于stack exchange,提问作者BugsOverflow
相关产品推荐
相关产品推荐

