删除sql中基于两个group by列的重复数据

删除sql中基于两个group by列的重复数据,sql,sql-server,Sql,Sql Server,嗨,我有一个Sql表,其中有一些重复的数据 id T_PSProjectId T_CodePattern T_NCR 1 0000130586 3MEXCM NULL 2 0000131771 3MEXTPS NULL 3 0000131771 3MEXTPS 123456 4 0000130586 3MRA NULL 5 0000130586 3MRAS NULL 6 0000130586 3MT1 NULL 7 000013

嗨,我有一个Sql表,其中有一些重复的数据

id  T_PSProjectId   T_CodePattern   T_NCR
1   0000130586  3MEXCM  NULL
2   0000131771  3MEXTPS NULL
3   0000131771  3MEXTPS 123456
4   0000130586  3MRA    NULL
5   0000130586  3MRAS   NULL
6   0000130586  3MT1    NULL
7   0000130586  3MT1    555
8   0000131176  3MT1    NULL
9   0000131190  3MT1    NULL
10  0000130584  3MT1    NULL
11  0000130584  3MT1    333000
12  0000130584  3MT1S   NULL
13  0000131755  3MT1S   NULL
14  0000130586  3MT1S   NULL
15  0000130586  3MT1S   550000
16  0000129495  3TMEU   NULL
17  0000131186  3TTHS   NULL
18  0000130583  3UA1P   NULL
19  0000130584  3UEXCESC    NULL
20  0000130584  3UEXCESCS   NULL
21  0000130583  3UEXCI  NULL
22  0000130583  3UEXCIS NULL
如您所见,第3行和第4行的T_codepattern列的值相同 所以我想删除那些列T_psprojectd+T_CodePattern的重复组合以及T_NCR null值的行

从第2排和第3排开始,它应该去掉第2排 从第6行和第7行,应删除第6行

我的意思是删除第二列和第三列值相同但第四列中没有值的行


谢谢

SQL Server的一个很好的功能是能够拥有“可更新”的CTE。这意味着我们可以用语句标识
中的行,然后在delete中使用它

with todelete as (
      select t.*,
             count(*) over (partition by T_PSProjectId, T_CodePattern) as cnt
      from t
     )
delete from todelete
    where cnt > 1 and T_NCR is null;

这样做的目的是使用窗口函数
count(*)
计算具有相同值
T\u psprojectd
T\u CodePattern
的行数。然后,
deletation
语句删除存在重复项的行,第三列有一个
NULL
值。

SQL Server的一个很好的功能是具有“可更新”CTE的能力。这意味着我们可以用
语句标识
中的行,然后在delete中使用它

with todelete as (
      select t.*,
             count(*) over (partition by T_PSProjectId, T_CodePattern) as cnt
      from t
     )
delete from todelete
    where cnt > 1 and T_NCR is null;
    This logic might resolve your query    


    DECLARE
  lv_dup_check NUMBER;
BEGIN
  FOR REC IN
  ( SELECT * FROM avrajit
  )
  LOOP
    SELECT COUNT(*)
    INTO lv_dup_check
    FROM avrajit -- to check whether duplicate exist or not
    WHERE name =rec.name
    AND salary = rec.salary;
    dbms_output.put_line(lv_dup_check);
    IF rec.department IS NULL AND lv_dup_check > 1 THEN -- if duplicate exists and the 3rd col null
      dbms_output.put_line ('deleted row'||' '||rec.salary);
      DELETE
      FROM avrajit --delete the 3rd null column with same value
      WHERE name      =rec.name
      AND salary      = rec.salary
      AND department IS NULL;
    END IF;
  END LOOP;
END;
这样做的目的是使用窗口函数
count(*)
计算具有相同值
T\u psprojectd
T\u CodePattern
的行数。
deletation
语句然后删除重复的行,第三列的值为
NULL

    This logic might resolve your query    


    DECLARE
  lv_dup_check NUMBER;
BEGIN
  FOR REC IN
  ( SELECT * FROM avrajit
  )
  LOOP
    SELECT COUNT(*)
    INTO lv_dup_check
    FROM avrajit -- to check whether duplicate exist or not
    WHERE name =rec.name
    AND salary = rec.salary;
    dbms_output.put_line(lv_dup_check);
    IF rec.department IS NULL AND lv_dup_check > 1 THEN -- if duplicate exists and the 3rd col null
      dbms_output.put_line ('deleted row'||' '||rec.salary);
      DELETE
      FROM avrajit --delete the 3rd null column with same value
      WHERE name      =rec.name
      AND salary      = rec.salary
      AND department IS NULL;
    END IF;
  END LOOP;
END;