2012-07-24 61 views
1

我已經想出了一個腳本來選擇「主」帳戶和「從」帳戶。公司名稱和郵政編碼完全匹配。它認爲最近更新的帳戶是主人。用於合併客戶帳戶的SQL腳本

select 
    m.ev870_acct_code, m.ev870_company_name, m.ev870_postal_code, m.ev870_iacvb_code, 
    s.ev870_acct_code, s.ev870_company_name, s.ev870_postal_code, s.ev870_iacvb_code 
from 
    ev870_acct_master m 
inner join 
    ev870_acct_master s 
on 
    m.ev870_company_name = s.ev870_company_name 
and m.ev870_postal_code = s.ev870_postal_code 
and m.ev870_upd_stamp > s.ev870_upd_stamp 
where 
    m.ev870_class = 'o' 
and s.ev870_class = 'o' 
and m.ev870_status != '0' 
and s.ev870_status != '0' 
and (m.ev870_iacvb_code = s.ev870_iacvb_code or isnull(m.ev870_iacvb_code,'') = '' or isnull(s.ev870_iacvb_code,'') = '') 
and s.ev870_company_name like '%council%' 
order by 
    m.ev870_upd_stamp desc 

用,因爲它代表了腳本的問題是,它可以判斷:

  • 賬戶1是主副本從賬戶2存在。
  • 賬戶1是主賬戶,存在重複的從賬戶3。
  • 賬戶2是主賬戶,存在重複的從賬戶3。

正如你所看到的,每一步的結果都會影響下一步。你能推薦一個更聰明的查詢嗎?

編輯解決方案:

select 
    m.ev870_acct_code, m.ev870_company_name, m.ev870_postal_code, m.ev870_iacvb_code, 
    s.ev870_acct_code, s.ev870_company_name, s.ev870_postal_code, s.ev870_iacvb_code 
from 
    ev870_acct_master s 
inner join 
    (
    select 
     ev870_acct_code, ev870_company_name, ev870_postal_code, ev870_iacvb_code, ev870_upd_stamp 
     ,row_number() over (partition by ev870_company_name, ev870_postal_code, ev870_iacvb_code order by ev870_upd_stamp desc) as howRecent 
    from 
     ev870_acct_master 
    where 
     ev870_class = 'o' 
    and ev870_status != '0' 
    and ev870_postal_code != '' 
    and ev870_company_name like 'A%' 
    ) m 
on 
    m.ev870_company_name = s.ev870_company_name 
and m.ev870_postal_code = s.ev870_postal_code 
and m.ev870_upd_stamp > s.ev870_upd_stamp 
where 
    m.howRecent = 1 
and m.ev870_iacvb_code = s.ev870_iacvb_code 
and s.ev870_class = 'o' 
and s.ev870_status != '0' 
+0

你在什麼版本的SQL Server? – iruvar 2012-07-24 14:22:20

+0

@Lee你能解釋一下你試圖用這個查詢完成什麼嗎? – kristof 2012-07-24 14:24:49

+0

SQL Server 2005. @kristof我在我的應用程序中識別重複的帳戶,然後將合併到一個帳戶。 – 2012-07-24 14:28:16

回答

0

跟隨您的評論:

@kristof我很確定我的應用程序重複帳戶這 屆時將合併爲一個賬戶。

您可以使用類似下面的代碼:

declare @dupExample table (
    id int identity(1,1) 
    ,name varchar(50) 
    ,postal varchar(50) 
    ,lastUpdated datetime 
) 

insert into @dupExample(name, postal, lastUpdated) 
values 
    ('a','pc1','20120101') 
    ,('a','pc1','20120501') 
    ,('a','pc1','20120601') 
    ,('a','pc1','20120701') 
    ,('a','pc1','20120201') 
    ,('b','pc2','20120102') 
    ,('b','pc2','20120202') 
    ,('b','pc2','20120302') 
    ,('b','pc2','20120302') 
    ,('c','pc2','20120302') 
    ,('d','pc2','20120302') 
    ,('d','pc2','20120302') 


select * from @dupExample 


/* 
    to see duplicates along with how recent they are 
*/ 
select 
    * 
    ,row_number() over (partition by name, postal order by lastUpdated) as howRecent 
from 
    @dupExample  

/* 
    delete duplicates leaving only most recent record based on date updated 
    WARNING only one record will be left for each dup even if there are multiple records 
    updated on the same date (see b, d examples) 
*/ 
delete de 
from @dupExample de 
    inner join 
    (select 
     id 
     ,row_number() over (partition by name, postal order by lastUpdated desc) as howRecent 
     from 
      @dupExample  
    ) der on de.id = der.id 
where 
    der.howRecent > 1 

/* 
    after delete 
*/  
select * from @dupExample 

,如果你有相同的dateUpdated副本你可以通過部分添加一些額外的標準來爲了指定這樣要刪除的記錄情景 - 但希望它給你一個很好的起點。

+0

偉大 - 謝謝,這不是我想做的事 - 但基本上給了我我的解決方案(請參閱我的問題中的說明)。 – 2012-07-24 19:21:04