SQL连接和更新删除重复项

时间:2017-01-27 05:20:52

标签: sql-server join sql-update

我需要帮助编写更新查询以根据匹配的表更新主表以删除重复项

源表: tblMatch(此匹配表用于根据每行的最新日期比较和选择客户)

  RowID  CustID MatchID Date    MatchDate   CustName    CustMatchName
    1       1       2    17-Jan  18-Jan     Joe         Joe
    2       1       3    17-Jan  19-Jan     Joe         Joe
    3       1       4    17-Jan  20-Jan     Joe         Joe
    4       5       4    21-Jan  20-Jan     Joe         Joe
    5       6       5    22-Jan  21-Jan     Joe         Joe
    7       50      55   01-Jan  02-Jan     Alice       Alice        

如果我们查看每一行,custid和matchid的关系是有效的,因为它们的名称是相同的。此表设计无法更改。

现在我面临的挑战是这5条记录实际上都是一个客户的副本。 1->2,1->3,1->4,5->4,6->5 1->5,1->6(链接)。

因此,我不想从具有最新日期的第一行决定并最终确定,而是想进行扫描并确定所有关系(如果有的话)(在这种情况下,1或2可以在下面的行中有子项),最后具有最新日期的客户应在其所有匹配的ID中处于活动状态,其余全部应处于非活动状态。

以下是我的detsination表。结果我期待在此选择和更新过程之后,CustID 6将remian Active = 1 rest all应更新为在下面的tblCustomer中的Active = 0。

tblCustomer(目标表)

CustID  CustName    Active
1       Joe         1
2       Joe         1
3       Joe         1
4       Joe         1
5       Joe         1
6       Joe         1
50      Alice       1
55      Alice       1
100     John        1
101     Nancy       1

2 个答案:

答案 0 :(得分:0)

试试这个:

这将更新字段active = 0并保留每个客户名称的最新CustID active = 1。

Create Procedure proc_update_duplicate as 
begin
)
DECLARE @custid nvarchar(30)
DECLARE @custname nvarchar(50)


 DECLARE Customer_cursor CURSOR FOR  select CustID,CustName from
(select max(CustID) as CustID,CustName as CustName from tblCustomer group by CustName) as a

 OPEN Customer_cursor  
    FETCH NEXT FROM Customer_cursor INTO @custid, @custname
  IF @@FETCH_STATUS <> 0   
        PRINT '         <<None>>'       

    WHILE @@FETCH_STATUS = 0  
    BEGIN  

       update tblCustomer set active = 0 where CustID != @custid and custName = @CustName


       FETCH NEXT FROM Customer_cursor INTO @custid,@CustName
   END  
   CLOSE Customer_cursor  
    DEALLOCATE Customer_cursor  
END   

要运行 exec proc_update_duplicate

答案 1 :(得分:0)

尝试这个...只是一起砍死...结果中额外的fieds所以你可以看到思想训练...我应该停止这个深夜的东西......呃......

编辑:已更新,以显示如何更新客户表...

CREATE TABLE #tblMatch (
    [RowID] [int] IDENTITY(1,1) NOT NULL,
    [CustID] [int] NULL,
    [MatchID] [int] NULL,
    [Date] [date] NULL,
    [MatchDate] [date] NULL,
    [CustName] [nvarchar](100) NULL,
    [CustMatchName] [nvarchar](100) NULL)

CREATE TABLE #tblCustomer (
    [RowID] [int] IDENTITY(1,1) NOT NULL,
    [CustID] [int] NULL,
    [CustName] [nvarchar](100) NULL,
    [Status] [bit] NULL)

INSERT INTO #tblMatch
    SELECT 1,2,CAST('17-Jan-2017' AS date),CAST('18-Jan-2017' AS date),'Joe','Joe'
        UNION
    SELECT 1,3,CAST('17-Jan-2017' AS date),CAST('19-Jan-2017' AS date),'Joe','Joe'
        UNION
    SELECT 1,4,CAST('17-Jan-2017' AS date),CAST('20-Jan-2017' AS date),'Joe','Joe'
        UNION
    SELECT 5,4,CAST('21-Jan-2017' AS date),CAST('20-Jan-2017' AS date),'Joe','Joe'
        UNION
    SELECT 6,5,CAST('22-Jan-2017' AS date),CAST('21-Jan-2017' AS date),'Joe','Joe'
        UNION
    SELECT 1,4,CAST('17-Jan-2017' AS date),CAST('20-Jan-2017' AS date),'Sam','Sam'
        UNION
    SELECT 5,4,CAST('23-Jan-2017' AS date),CAST('25-Jan-2017' AS date),'Sam','Sam'
        UNION
    SELECT 6,5,CAST('22-Jan-2017' AS date),CAST('21-Jan-2017' AS date),'Sam','Sam'

INSERT INTO #tblCustomer
    SELECT 1,'Joe',1
        UNION
    SELECT 2,'Joe',1
        UNION
    SELECT 3,'Joe',1
        UNION
    SELECT 4,'Joe',1
        UNION
    SELECT 5,'Joe',1
        UNION
    SELECT 6,'Joe',1
        UNION
    SELECT 1,'Sam',1
        UNION
    SELECT 4,'Sam',1
        UNION
    SELECT 5,'Sam',1
        UNION
    SELECT 6,'Sam',1

SELECT [CustID],[CustName],[Status] FROM #tblCustomer
ORDER BY [CustName],[CustID]

UPDATE tCust SET
    tCust.[Status] = tt.[Active]
FROM #tblCustomer tCust INNER JOIN (
    SELECT [CustID],[Date],[CustName],(SELECT MAX(CASE WHEN [Date]>=[MatchDate] THEN [Date] ELSE [MatchDate] END) FROM #tblMatch WHERE [CustName] = t.[CustName] OR [CustMatchName] = t.[CustName]) AS [mxDT],
    CASE WHEN (SELECT MAX(CASE WHEN [Date]>=[MatchDate] THEN [Date] ELSE [MatchDate] END) FROM #tblMatch WHERE [CustName] = t.[CustName] OR [CustMatchName] = t.[CustName]) = [Date] THEN 1 ELSE 0 END AS [Active]
    FROM (
        SELECT DISTINCT cust.[CustID],cust.[Date],cust.[CustName]
        FROM #tblMatch cust INNER JOIN #tblMatch match
        ON cust.[CustName] = match.[CustMatchName]
            UNION
        SELECT DISTINCT match.[MatchID] AS [CustID],match.[MatchDate] AS [Date],match.[CustMatchName] AS [CustName]
        FROM #tblMatch cust INNER JOIN #tblMatch match
        ON cust.[CustName] = match.[CustMatchName]
        ) t
    ) tt
ON tCust.[CustID] = tt.[CustID]

SELECT [CustID],[CustName],[Status] FROM #tblCustomer
ORDER BY [CustName],[CustID]

DROP TABLE #tblCustomer
DROP TABLE #tblMatch