0

我試圖結合基於ClusterID的記錄,以獲得豐富的客戶記錄數據。如何使用MS SQL將以下內容分組?由於記錄需要位於同一行以使其工作,因此合併不起作用,並且如果每個羣集有超過2個匹配,則這將是一項繁瑣的處理。在所有列中使用通過ClusterId的max是我正在做的解決方法,但我希望有一個更有效的方法來做到這一點。刪除併合並客戶數據

有:

ClusterID,CustomerNo,Name,Email,Mobile,Address,PostalCode,Passport,ProfileNo 
100,NULL,Person,[email protected],NULL,OfficeAdd,12345,NULL,123 
100,456,Person,[email protected],98765,HomeAdd,34567,P12345,NULL 

**這是SSIS DQS匹配節點(https://ssisdqsmatching.codeplex.com/)的結果。它可以進行比賽,但不能處理生存部分以獲得金牌。

旺旺:

ClusterID,CustomerNo,Name,Email,Mobile,Address,PostalCode,Passport,ProfileNo 
100,456,Person,[email protected],98765,OfficeAdd,12345,P12345,123 

任何想法,將不勝感激。謝謝!

回答

1
DECLARE @CLUSTERID VARCHAR(MAX),@CUSTOMERNO VARCHAR(MAX),@NAME VARCHAR(MAX),@EMAIL VARCHAR(MAX),@MOBILE VARCHAR(MAX) 
DECLARE @POSTALCODE VARCHAR(MAX),@PASSPORT VARCHAR(MAX),@PROFILENO VARCHAR(MAX),@ADDRESS VARCHAR(MAX) 

DECLARE @NCLUSTERID VARCHAR(MAX),@NCUSTOMERNO VARCHAR(MAX),@NNAME VARCHAR(MAX),@NEMAIL VARCHAR(MAX),@NMOBILE VARCHAR(MAX) 
DECLARE @NPOSTALCODE VARCHAR(MAX),@NPASSPORT VARCHAR(MAX),@NPROFILENO VARCHAR(MAX),@NADDRESS VARCHAR(MAX) 
DECLARE @NEW_TABLE TABLE ( ClusterID varchar(max) , 
    CustomerNo varchar(max) , 
    Name varchar(max) , 
    Email varchar(max) , 
    Mobile varchar(max) , 
    Address varchar(max) , 
    PostalCode varchar(max) , 
    Passport varchar(max) , 
    ProfileNo varchar(max) 
) 

DECLARE C CURSOR FOR 
SELECT DISTINCT CLUSTERID FROM CUSTOMER 
OPEN C 
FETCH NEXT FROM C INTO @CLUSTERID 
WHILE @@FETCH_STATUS=0 
BEGIN 

    DECLARE D CURSOR FOR 
    select CustomerNo,Name,Email,Mobile,Address,PostalCode,Passport,ProfileNo from customer where [email protected] 
    OPEN D 
    FETCH NEXT FROM D INTO @CustomerNo,@Name,@Email,@Mobile,@Address,@PostalCode,@Passport,@ProfileNo 
    WHILE @@FETCH_STATUS=0 
    BEGIN 

    IF @CustomerNo is not null SET @[email protected] 
    IF @CustomerNo IS NOT NULL SET @NCustomerNo= @CustomerNo ; 
    IF @Name  IS NOT NULL SET @NName  = @Name  ; 
    IF @Email  IS NOT NULL SET @NEmail  = @Email  ; 
    IF @Mobile  IS NOT NULL SET @NMobile = @Mobile  ; 
    IF @Address IS NOT NULL SET @NAddress = @Address ; 
    IF @PostalCode IS NOT NULL SET @NPostalCode= @PostalCode ; 
    IF @Passport IS NOT NULL SET @NPassport = @Passport ; 
    IF @ProfileNo IS NOT NULL SET @NProfileNo = @ProfileNo ; 

    FETCH NEXT FROM D INTO @CustomerNo,@Name,@Email,@Mobile,@Address,@PostalCode,@Passport,@ProfileNo 
    END 
    CLOSE D 
    DEALLOCATE D 
     INSERT INTO @NEW_TABLE VALUES (
     @CLUSTERID, 
     @NCustomerNo , 
     @NName   , 
     @NEmail  , 
     @NMobile  , 
     @NAddress  , 
     @NPostalCode , 
     @NPassport  , 
     @NPROFILENO  
     ) 



FETCH NEXT FROM C INTO @CLUSTERID 
END 
CLOSE C 
DEALLOCATE C 


SELECT * FROM @NEW_TABLE 
+0

試試這個,我想這可以解決您的問題 –

+0

謝謝!這是我正在尋找的東西。我是否正確,它會轉到每個ClusterID的循環,然後填充字段時,他們不是NULL? – mtryingtocode

+0

此外,爲了在循環中增加一些複雜性,我們假設對於地址字段,第1行地址不爲空且郵政爲空,則第2行地址和郵政不爲空。我怎樣才能添加一個條件,我不會在第二排郵政編碼?因爲如果我在第2行中選取郵政編碼,則與第1行地址結合時,它會給我提供不正確的信息。 – mtryingtocode

0

我想這個查詢解決您的要求

DECLARE @CLUSTERID VARCHAR(MAX),@CUSTOMERNO VARCHAR(MAX),@NAME VARCHAR(MAX),@EMAIL VARCHAR(MAX),@MOBILE VARCHAR(MAX) 
DECLARE @POSTALCODE VARCHAR(MAX),@PASSPORT VARCHAR(MAX),@PROFILENO VARCHAR(MAX),@ADDRESS VARCHAR(MAX) 

DECLARE @NCLUSTERID VARCHAR(MAX)=NULL,@NCUSTOMERNO VARCHAR(MAX)=NULL,@NNAME VARCHAR(MAX)=NULL,@NEMAIL VARCHAR(MAX)=NULL,@NMOBILE VARCHAR(MAX)=NULL 
DECLARE @NPOSTALCODE VARCHAR(MAX)=NULL,@NPASSPORT VARCHAR(MAX)=NULL,@NPROFILENO VARCHAR(MAX)=NULL,@NADDRESS VARCHAR(MAX)=NULL 
DECLARE @NEW_TABLE TABLE ( ClusterID varchar(max) , 
    CustomerNo varchar(max) , 
    Name varchar(max) , 
    Email varchar(max) , 
    Mobile varchar(max) , 
    Address varchar(max) , 
    PostalCode varchar(max) , 
    Passport varchar(max) , 
    ProfileNo varchar(max) 
) 

DECLARE C CURSOR FOR 
SELECT DISTINCT CLUSTERID FROM CUSTOMER 
OPEN C 
FETCH NEXT FROM C INTO @CLUSTERID 
WHILE @@FETCH_STATUS=0 
BEGIN 

    DECLARE D CURSOR FOR 
    select CustomerNo,Name,Email,Mobile,Address,PostalCode,Passport,ProfileNo from customer where [email protected] 
    OPEN D 
    FETCH NEXT FROM D INTO @CustomerNo,@Name,@Email,@Mobile,@Address,@PostalCode,@Passport,@ProfileNo 
    WHILE @@FETCH_STATUS=0 
    BEGIN 

    IF @CustomerNo is not null SET @[email protected] 
    IF @CustomerNo IS NOT NULL SET @NCustomerNo= @CustomerNo ; 
    IF @Name  IS NOT NULL SET @NName  = @Name  ; 
    IF @Email  IS NOT NULL SET @NEmail  = @Email  ; 
    IF @Mobile  IS NOT NULL SET @NMobile = @Mobile  ; 
    IF @Passport IS NOT NULL SET @NPassport = @Passport ; 
    IF @ProfileNo IS NOT NULL SET @NProfileNo = @ProfileNo ; 

    IF (@ADDRESS IS NOT NULL AND @NADDRESS IS NOT NULL) 
    BEGIN 
    SET @NAddress = @Address ; 
    SET @NPostalCode= @PostalCode ; 
    END 
    ELSE IF(@ADDRESS IS NOT NULL AND @NADDRESS IS NULL) 
    BEGIN 
    SET @NAddress = @Address ; 
    SET @NPostalCode= NULL ; 
    END 
    ELSE IF(@ADDRESS IS NULL AND @NADDRESS IS NOT NULL) 
    BEGIN 
    SET @NAddress = NULL ; 
    SET @NPostalCode= @PostalCode ; 
    END 


    FETCH NEXT FROM D INTO @CustomerNo,@Name,@Email,@Mobile,@Address,@PostalCode,@Passport,@ProfileNo 
    END 
    CLOSE D 
    DEALLOCATE D 

     INSERT INTO @NEW_TABLE VALUES (
     @CLUSTERID, 
     @NCustomerNo , 
     @NName   , 
     @NEmail  , 
     @NMobile  , 
     @NAddress  , 
     @NPostalCode , 
     @NPassport  , 
     @NPROFILENO  
     ) 



FETCH NEXT FROM C INTO @CLUSTERID 
END 
CLOSE C 
DEALLOCATE C 


SELECT * FROM @NEW_TABLE