根据分组依据和其他条件更新列

时间:2020-01-13 17:43:23

标签: sql oracle

我这样做的目的是基于列中的相同值来识别重复记录。

update table1
set reason = 'duplicate'
where id exists in (select min(id)
                    from table 1
                    where company ='Bestbuy'
                    and account = '123456'
                    group by company, account, date, price, member_id, amount)
and company = 'Bestbuy'
and account = '123456'

因此,这会将所有重复记录分组在一起,并更新“原因”列以表示它是重复记录。现在,我试图解决一种情况,可以按所有这些列进行分组,但是,如果“销售代表”不同,我只想“排除”记录。

因此,使用此示例表:

table

因此,例如,第1,第4,第5,第9和第10位将按公司,帐户,日期,价格和member_ID分组。不过,销售代表将会改变。我正在尝试根据第一个分组创建一个子分组,它将更新该分组中具有不同销售代表的记录。

所以最后,第10条记录将被排除。

1 个答案:

答案 0 :(得分:1)

您似乎想要:

update table1
         set reason = 'duplicate'
where exists (select 1
              from table1 t1 
              where t1.company = table1.company and 
                    t1.account = table1.account and
                    t1.date = table1.date and 
                    t1.price = table1.price and
                    t1.member_id =  table1.member_id and
                    t1.amount = table1.amount and
                    t1.sales_rep <> table1.sales_rep
             ) and 
      company = 'Bestbuy' and 
      account = '123456';