我需要删除表格中出现的重复项,但是我不知道该怎么做。
这些重复项中的一个通过ID链接到另一个表。我想保留此记录,并删除所有具有相同值的其他记录。
主表:ZasobCRM 第二张表:ZasobCRM_Raw
按时间将读取器中的数据发送到表ZasobCRM_Raw。然后,基于同一天的两个条目,为同一员工在表中创建一个条目:ZasobCRM。
有效示例:
select * from ZasobCRM_Raw where ZasobCRM_ID = 20490
select * from ZasobCRM where ID = 20490
结果:
ID DataIMPORT DataUTWORZENIA string_1 string_2 string_3 string_4 Status ZasobCRM_ID Exception
45883 2019-10-01 17:08:18.000 2019-08-01 05:57:54.000 0025 6531487 010819055634 0 WCZYTANE 20490
45893 2019-10-01 17:08:18.000 2019-08-01 06:57:55.000 0025 6531487 010819065321 0 WCZYTANE 20490
ID Zad_Guid Operator_ID Rozpoczecie Zakonczenie Nazwa
20490 c2db3bf6-6156-4dba-b802-b4064f99d7ca 2181 2019-08-01 05:56:34.000 2019-08-01 06:53:21.000 Arkadiusz Kowalczyk
无效的示例(重复):
select * from ZasobCRM_Raw where string_1 = '0044' and string_3 like '%29101905%' order by string_3
select * from ZasobCRM where Rozpoczecie = '2019-10-29 05:55:48.000' order by ID
结果:
ID DataIMPORT DataUTWORZENIA string_1 string_2 string_3 string_4 Status ZasobCRM_ID Exception
58020 2019-11-07 09:22:31.000 2019-10-29 05:56:38.000 0044 6531592 291019055548 0 WCZYTANE 25143
ID Zad_Guid Operator_ID Rozpoczecie Zakonczenie Nazwa
24596 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24610 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24627 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24641 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24658 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24672 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24694 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24708 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24725 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24740 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24757 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24771 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24788 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24802 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24819 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24833 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24850 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24864 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24881 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24895 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24912 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24926 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24943 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24957 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24974 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
24988 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25005 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25019 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25036 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25050 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25067 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25081 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25098 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25112 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25129 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
25143 0163c7af-16ef-4289-8a6c-e4d47c3e8dbe 2199 2019-10-29 05:55:48.000 2019-10-29 11:45:16.000 Ewa Domin
在上一个示例中,我没有在表中提供第二行:ZasobCRM_Raw,因为这无关紧要。 在上一个示例中,删除后,我应该只有ID为25143的行
我能够通过查询找到这些重复项:
SELECT
Zad_Guid, Operator_ID, Rozpoczecie, Zakonczenie, Nazwa, COUNT(*)
FROM
ZasobCRM as ZCRM
GROUP BY
Zad_Guid, Operator_ID, Rozpoczecie, Zakonczenie, Nazwa
HAVING
COUNT(*) > 1
我的查询没有向我显示这些重复行中的哪些与另一个表相关联。
我不知道如何基于此删除它们。
编辑:
delete from ZasobCRM
WHERE Zad_Guid IN (SELECT
Zad_Guid
FROM
ZasobCRM as ZCRM
GROUP BY
Zad_Guid, Operator_ID, Rozpoczecie, Zakonczenie, Nazwa
HAVING
COUNT(*) > 1)
and
(select count(*) from ZasobCRM_Raw as ZRaw where ZRaw.ZasobCRM_ID = ZasobCRM.ID) = 0
工作!
答案 0 :(得分:0)
您只需在Delete命令中创建一个子查询。
DELETE
FROM <tablename>
WHERE Zad_Guid IN (SELECT
Zad_Guid
FROM
ZasobCRM as ZCRM
GROUP BY
Zad_Guid, Operator_ID, Rozpoczecie, Zakonczenie, Nazwa
HAVING
COUNT(*) > 1)
注意:在执行“删除”命令之前,请确保您已经退赛。