使用Distinct从表A插入表B

时间:2012-06-26 15:41:16

标签: sql-server sql-server-2008 tsql

我有2张桌子

表A

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City  State 
NULL      Micheal    Calvin      Dodson    12     23     1234  123  XYZ   ABC    
NULL      John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG    
NULL      John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG 
NULL      William    Stephen     NULL      45     NULL   NULL  NULL HJD   ABC
NULL      Victor     NULL        Anthony   NULL   NULL   NULL  NULL NULL  NULL

表B

NameID   FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City State Zip Email Gender...

我需要获取表A的(FirstName,MiddleName,LastName)的不同记录,并将与表A匹配的其他字段插入表B中。 我的表B将NameID作为身份证书。因此,在将唯一记录插入表B后,我需要获取该NameID并将其插回到下面的表A中:

表A

表A

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City  State 
1         Micheal    Calvin      Dodson    12     23     1234  123  XYZ   ABC    
2         John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG    
2         John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG 
3         William    Stephen     NULL      45     NULL   NULL  NULL HJD   ABC
4         Victor     NULL        Anthony   NULL   NULL   NULL  NULL NULL  NULL

表B

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City State Zip  Email Gender...
1       Micheal    Calvin      Dodson    12     23     1234  123   XYZ   ABC   NULL NULL  NULL
2       John       NULL        Keith     NULL   NULL   2344  NULL  SQE   FDG   NULL NULL  NULL
3       William    Stephen     NULL      45     NULL   NULL  NULL  HJD   ABC   NULL NULL  NULL
4       Victor     NULL        Anthony   NULL   NULL   NULL  NULL  NULL  NULL  NULL NULL  NULL

你能帮我解决这个问题。我无法正确查询此查询。 SQL Server 2008中的代码

提前致谢,
Sunitha

2 个答案:

答案 0 :(得分:0)

我认为最简单的方法是使用两个查询。第一个问题是在TableA中处理重复项。以下查询为每个名称组合选择一个任意行:

插入TableB()        选择 ()        从(选择a。*,                     row_number()over(由FirstName分区,MiddleName,LastName order by FirstName)为seqnum              来自TableA a             ) 一个         其中seqnum = 1

然后,更新原始表:

update TableA
    set NameId = (select max(NameId) from TableB
                  where TableB.FirstName = TableA.FirstName and
                        TableB.MiddleName = TableA.MiddleName and
                        TableB.LastName = TableA.LastName
                 )
    where NameId is null

如果您的字段包含NULL值(而不是空格),则可以使用coalesce()作为连接条件:

update TableA
    set NameId = (select max(NameId) from TableB
                  where coalesce(TableB.FirstName, '<null>') = coalesce(TableA.FirstName, '<null>') and
                        coalesce(TableB.MiddleName, '<null>') = coalesce(TableA.MiddleName, '<null>') and
                        coalesce(TableB.LastName , '<null>')= coalesce(TableA.LastName, '<null>')
                 )
    where NameId is null

答案 1 :(得分:0)

DECLARE @results TABLE
(
  NameID INT, 
  FirstName VARCHAR(32), -- guessing on data types for these columns 
  MiddleName VARCHAR(32), 
  LastName VARCHAR(32)
);

;WITH x AS 
(
 SELECT FirstName, MiddleName, LastName, 
     rn = ROW_NUMBER() OVER (PARTITION BY FirstName, MiddleName, LastName
      ORDER BY (SELECT NULL)
   ) --, ... other columns ...
   FROM dbo.TableA
)
INSERT dbo.TableB 
(
  FirstName, MiddleName, LastName --, ... other columns ...
)
OUTPUT 
  inserted.NameID, inserted.FirstName, 
  inserted.MiddleName, inserted.LastName
INTO @results
SELECT FirstName, MiddleName, LastName --, ... other columns ...
FROM x WHERE rn = 1;

UPDATE a SET NameID = r.NameID
  FROM dbo.TableA AS a
  INNER JOIN @results AS r
  ON COALESCE(a.FirstName,'') = COALESCE(r.FirstName,'')
  AND COALESCE(a.MiddleName,'') = COALESCE(r.MiddleName,'')
  AND COALESCE(a.LastName,'') = COALESCE(r.LastName,'');