使用group by子查询对重复记录集进行重复数据删除

时间:2013-06-28 00:24:03

标签: oracle group-by subquery min

我想知道如何使用类似以下的查询将活动ID返回到父查询以删除父记录集。这里的问题是使用这个组或不同我找不到办法。

我将使用所有group by字段来确定唯一记录。但是,我只需要使用select min(status.effective_date)

的记录

查询返回正确的日期值,但我无法仅使用该日期值将其链接回父项活动记录。

select min(status.effective_date) 
  from accounts 
     , address 
     , activity 
     , status 
 where accounts.par_row_id = activity.account_id 
   and address.row_id = activity.address_id
   and status.par_row_id = activity.status_id                                        
   and account.name = 'xyz'
 group by account.name, address.addr, address.ADDR_LINE_2, address.ADDR_LINE_3
        , address.ADDR_LINE_4, address.CITY, address.COUNTRY, address.X_STATE
        , address.ZIPCODE

1 个答案:

答案 0 :(得分:0)

您应该尝试此查询:

select min(activity.row_id) keep(dense_rank first order by status.effective_date) as activity_id
from accounts
, address
, activity
, status
where accounts.par_row_id = activity.account_id 
and address.row_id = activity.address_id
and status.par_row_id = activity.status_id
and accounts.name = 'xyz'
group by accounts.name, address.addr, address.ADDR_LINE_2, address.ADDR_LINE_3
    , address.addr_line_4, address.city, address.country, address.x_state
    , address.ZIPCODE;