2017-01-16 54 views
2

我有一個表成員,其中他們是重複的條目,我想刪除其中的一個,但1的條目有一些列更新,另一個有一些其他。更新從同一個表中值爲空的項目

所以我想要做的是更新,但其中的值存在於1和null或另一個空,以便兩行變得完全相同,我不會丟失任何數據。

表結構:

CREATE TABLE [dbo].[membermobilenumberisnull](
[TransId] [bigint] NOT NULL, 
[member_id] [int] NOT NULL, 
[gendertype] [int] NULL, 
[relationship_rs_code] [nvarchar](2) NULL, 
[ration_card_id] [int] NOT NULL, 
[uid] [nvarchar](16) NULL, 
[member_dob] [datetime] NULL, 
[member_name_en] [nvarchar](150) NULL, 
[mother_name_en] [nvarchar](150) NULL, 
[father_name_en] [nvarchar](150) NULL, 
[member_age] [smallint] NULL, 
[nationality] [nvarchar](150) NULL, 
[MobileNumber] [nvarchar](20) NULL, 
[IsUpdated] [bigint] NULL, 
[UpdationDate] [datetime] NULL, 
[IsDeleted] [bigint] NULL, 
[DeletionDate] [datetime] NULL, 
[CreationDate] [datetime] NULL, 
[UpdatedBy] [nvarchar](250) NULL, 
[DeletedBy] [nvarchar](250) NULL, 
[MobileNumber1] [nvarchar](20) NULL, 
[MobileFlag] [nvarchar](250) NULL 
) ON [PRIMARY] 

GO 

樣本數據

TransId  member_dob member_name_en  member_age nationality MobileNumber UpdationDate CreationDate 
252238402 12-09-1985 PUSHPINDER SINGH 31   IND       NULL   30-07-2016 
252238403 12-09-1985 PUSHPINDER SINGH 31   IND   8626934377  NULL   30-07-2016 
260846102 03-06-1984 VUDDHI DEVI   32   IND   9459209701  19-10-2016  14-08-2016 
260846105 03-06-1984 VUDDHI DEVI   32   IND   NULL       14-08-2016 

預期結果:

TransId  member_dob member_name_en  member_age nationality  MobileNumber UpdationDate CreationDate 
252238402 12-09-1985 PUSHPINDER SINGH 31   IND    8626934377  NULL   30-07-2016 
252238403 12-09-1985 PUSHPINDER SINGH 31   IND    8626934377  NULL   30-07-2016 
260846102 03-06-1984 VUDDHI DEVI   32   IND    9459209701  19-10-2016  14-08-2016 
260846105 03-06-1984 VUDDHI DEVI   32   IND    9459209701  19-10-2016  14-08-2016 

臨屋提前NKS

舉例附: enter image description here

+1

請編輯您的問題以包含相關表格DDL,一些示例數據作爲DML和所需結果。 –

+0

@Zohar已完成請檢查 –

+0

「主鍵是多個項目的組合鍵」。這很奇怪。理想情況下,成員表的主鍵應該是成員名稱或成員編號。我假設我們在這裏談論一些交易表?應該有一個真正的成員表和一個適當的主鍵。您可以使用您的交易記錄來插入成員表。可以通過程序循環遍歷數據或觸發器,也可以通過'member_name_en'查詢分組並指定每個字段的適當聚合函數,例如, 'MAX(移動電話號碼)'。 –

回答

1

假設你的組合PK是member_dob,member_name_en,member_age

UPDATE M1 
SET  M1.MobileNumber  = ISNULL(NULLIF(M1.MobileNumber,''),M2.MobileNumber) 
     ,M1.UpdationDate = ISNULL(M1.UpdationDate,M2.UpdationDate) 
FROM Member AS M1 
    INNER JOIN Member AS M2 ON M1.TransId <> M2.TransId 
           AND M1.member_dob = M2.member_dob 
           AND M1.member_name_en = M2.member_name_en 
           AND M1.member_age = M2.member_age 
+0

不起作用:( –

+0

可以üPLZ解釋WTS現在的問題,如果移動號碼字段包含空字符串,你可以使用(。 .ISNULL(NULLIF(M1.MobileNumber,''),M2.MobileNumber)..) –

+0

我做了,但我認爲它更新自己從相同的transid而不是另一個 –

0

試試這個

WITH t2 AS (
SELECT member_dob, member_name_en, member_age, nationality, MobileNumber 
FROM <<table>> 
WHERE MobileNumber IS NOT NULL 
) 
UPDATE t1 
SET MobileNumber = t2.MobileNumber 
FROM <<table>> t1 JOIN t2 
ON t1.member_dob = t2.member_dob 
AND t1.member_name_en = t2.member_name_en 
AND t1.member_age = t2.member_age 
AND t2.nationality = t2.nationality 
WHERE t1.MobileNumber IS NULL 
+0

不工作:(0行受影響 –

0

看來你想看看一個member_name_en和從其記錄中選取一個值, null over null。然後簡單地彙總並使用MINMAX挑選此類值。

update m 
    set member_dob = src.max_member_dob 
    , member_age = src.max_member_age 
    , nationality = src.max_nationality 
     etc. 
from membermobilenumberisnull m 
(
    select 
    member_name_en, 
    max(member_dob) as max_member_dob, 
    max(member_age) as max_member_age, 
    max(nationality) as max_nationality, 
    etc. 
    from membermobilenumberisnull 
    group by member_name_en 
) src on src.member_name_en = m.member_name_en; 
相關問題