我在网上搜索并没有找到这种答案。
我有表emp_master_data,它有很多列,但我想使用几列来过滤数据(选择查询),然后在分析后,我想删除这些记录。
过滤器应该应用于三列emp_card_no,emp_id,enrollment_exp_dt。员工可以多次注册,这意味着您将拥有多个具有相同emp_no,emp_id和相同/不同enrollment_exp_dt的记录。
现在,我需要这样做:
请让我知道我能做的最佳方式。我确实试过这样做,但没有解决所有问题。
SELECT *
FROM brm_staging A
WHERE EXISTS (
SELECT 1 FROM brm_staging
WHERE enrollment_exp_dt = A.enrollment_exp_dt
and emp_id= A.emp_id
and emp_card_no =A.emp_card_no
AND ROWID < A.ROWID
);
答案 0 :(得分:1)
DELETE FROM YOUR_TABLE T1
INNER JOIN (
SELECT T2.* FROM YOUR_TABLE T2,
(SELECT EMP_ID, CARD_NO, COUNT(*) FROM
YOUR_TABLE
GROUP BY EMP_ID, CARD_NO
HAVING COUNT(*) > 1) T3
WHERE T2.EMP_ID=T3.EMP_ID AND T2.CARD_NO = T3.CARD_NO AND
T2.ENROLLMENT_EXP_DT NOT IN (SELECT MAX(T4.ENROLLMENT_EXP_DT)
FROM YOUR_TABLE T4) T5 ON
T1.EMP_ID=T5.EMP_ID AND T1.CARD_NO=T5.CARD_NO AND T1.ENROLLMENT_EXP_DT=T5.ENROLLMENT_EXO_DT
(编辑)我认为这项工作也是如此(更简化)
DELETE FROM YOUR_TABLE T1
WHERE EXISTS (
SELECT T2.* FROM YOUR_TABLE T2,
(SELECT EMP_ID, CARD_NO, COUNT(*) FROM
YOUR_TABLE
GROUP BY EMP_ID, CARD_NO
HAVING COUNT(*) > 1) T3
WHERE T2.EMP_ID=T3.EMP_ID AND T2.CARD_NO = T3.CARD_NO AND
T2.ENROLLMENT_EXP_DT NOT IN (SELECT MAX(T4.ENROLLMENT_EXP_DT)
FROM YOUR_TABLE T4)