1

「マスター」アカウントと「スレーブ」アカウントを選択するスクリプトを考え出しました。会社名と郵便番号が完全に一致する場所。最後に更新されたアカウントをマスターと見なします。

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'
4

1 に答える 1

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の重複がある場合は、そのようなシナリオで削除するレコードを指定するために、部分的に並べ替えるためにいくつかの追加基準を追加できますが、うまくいけば、それが良い出発点になります.

于 2012-07-24T16:31:29.110 に答える