2012-11-02 35 views
1

我正在嘗試編寫將重複的客戶端記錄合併到單個記錄中的查詢。現在我只是建立一個映射表來知道我應該怎樣映射。從表格值函數的輸出中更新一組數據

這裏有兩個功能我寫來幫助我在這

CREATE FUNCTION FindDuplicateClients() 
RETURNS TABLE AS RETURN 
(
    select distinct CLIENT_GUID 
    from CLIENTS c 
    inner join 
    (
     select FIRST_NAME, LAST_NAME, HOME_PHONE 
     from CLIENTS 
     group by FIRST_NAME, LAST_NAME, HOME_PHONE 
     having COUNT(*) > 1 
    ) t on c.FIRST_NAME = t.FIRST_NAME and c.LAST_NAME = t.LAST_NAME and c.HOME_PHONE = t.HOME_PHONE) 
go 

--Find other clients that map to this client 
CREATE FUNCTION FindDuplicateClientsByClient (@Client uniqueidentifier) 
RETURNS TABLE AS RETURN 
(
    select distinct CLIENT_GUID 
    from CLIENTS c 
    inner join 
    (
     select x.FIRST_NAME, x.LAST_NAME, x.HOME_PHONE 
     from CLIENTS x 
     inner join 
     (
      select FIRST_NAME, LAST_NAME, HOME_PHONE 
      from CLIENTS 
      where CLIENT_GUID = @Client 
     ) y on x.FIRST_NAME = y.FIRST_NAME and x.LAST_NAME = y.LAST_NAME and x.HOME_PHONE = y.HOME_PHONE 
     group by x.FIRST_NAME, x.LAST_NAME, x.HOME_PHONE 
     having COUNT(*) > 1 
    ) t on c.FIRST_NAME = t.FIRST_NAME and c.LAST_NAME = t.LAST_NAME and c.HOME_PHONE = t.HOME_PHONE 
    where CLIENT_GUID <> @Client) 
go 

第一個函數返回成功具有超過1個記錄所有CLIENT_GUID的,你在一個GUID傳遞第二,它返回所有其他共享「公共信息」(在這種情況下,名字,姓氏和家庭電話)的GUID

問題是填寫我的映射表。我需要遵循一些規則來優先重複某些重複項。例如有人誰擁有了交易需要沒有自己CLIENT_GUID改變,但他們能夠有其他的GUID合併到它們(如果其他的GUID沒有交易)

--Create Mapping table 
select CLIENT_GUID, CAST(null as uniqueidentifier) as NEW_CLIENT_GUID 
into #mapping 
from FindDuplicateClients() 

--Do not map people who have transactions 
update #mapping 
set NEW_CLIENT_GUID = CLIENT_GUID 
where CLIENT_GUID in (select CLIENT_GUID from trnHistory) 

現在這裏是我運行陷入麻煩。我不知道如何獲取在上一個查詢中設置了NEW_CLIENT_GUID的人員的列表,並針對該GUID運行FindDuplicateClientsByClient,並將任何結果的NEW_CLIENT_GUID設置爲NEW_CLIENT_GUID,該值被輸入到函數而不使用遊標。

這裏是我想出了使用遊標

declare cur cursor LOCAL FAST_FORWARD for select NEW_CLIENT_GUID from #mapping where NEW_CLIENT_GUID is not null 
declare @NEW_CLIENT_GUID uniqueidentifier 

open cur 
fetch next from cur into @NEW_CLIENT_GUID 
while @@fetch_status = 0 
begin 
    update #mapping 
    set NEW_CLIENT_GUID = @NEW_CLIENT_GUID 
    where CLIENT_GUID in (select CLIENT_GUID from FindDuplicateClientsByClient(@NEW_CLIENT_GUID)) --Find duplicates to this record 
     and NEW_CLIENT_GUID is null --Do not reassign values that are already set (ie: duplicates that have transactions) 

    fetch next from cur into @NEW_CLIENT_GUID 
end 

close cur 
deallocate cur 

對我來說,遍歷每個結果在#mapping具有價值組似乎並沒有正確的給我的方法。什麼是正確的方法來做到這一點?我正在使用SQL Server 2008 R2,但我更喜歡它是否與SQL Server 2005兼容。

回答

0

這已經兩天了,沒有答案,我只是堅持使用我的遊標解決方案,因爲它足夠執行我需要做的事情。

當我不得不第二次循環查找未映射到前一遍的人時,我確實使用了不同的方法,但它只是一個while循環,其行爲與前一個遊標完全相同。

declare @tmpGuid uniqueidentifier 
select @tmpGuid = CLIENT_GUID from #mapping where NEW_CLIENT_GUID is null 
while @@ROWCOUNT > 0 
begin 
    --Set the first unset guid to itself 
    update #mapping 
    set NEW_CLIENT_GUID = @tmpGuid 
    where CLIENT_GUID = @tmpGuid 

    --set all other duplicates to the guid we just used. 
    update #mapping 
    set NEW_CLIENT_GUID = @tmpGuid 
    where CLIENT_GUID in (select CLIENT_GUID from FindDuplicateClientsByClient(@tmpGuid)) 
     and NEW_CLIENT_GUID is null 

    --get next guid 
    select @tmpGuid = CLIENT_GUID from #mapping where NEW_CLIENT_GUID is null 
end 
set nocount off 
go 
相關問題