2013-06-28 71 views
0

我想知道如何使用類似下面的查詢將活動Id返回到父查詢以去重複父記錄集。這裏的問題是,使用這個組或獨特我無法找到一種方法。使用group by subquery重複數據刪除記錄集

我將使用所有字段的組來確定唯一的記錄。但是,我只需要使用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

歡迎SO。請閱讀如何提出問題 - http://stackoverflow.com/questions/how-to-ask您需要包含相關的表模式,一些示例,數據和exptected輸出。 – OldProgrammer

+0

您需要選擇與GROUP BY相同的記錄...否則,您如何獲得想要獨特的數據? – Ben

回答

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;