修复SQL中的重复客户

时间:2012-03-25 15:19:39

标签: sql database duplicates

我在sql server上使用java,我遇到了重复客户的数据库问题  (许多客户以下列方式重复)。 我的表是:

CUSTOMER(person_id,customer_id)
ORDER (order_id,person_id )

并且重复的客户看起来像这样:

CUSTOMER(333,040535743)
CUSTOMER(334,040535743)
CUSTOMER(335,040535743)

ORDER (1,333 )
ORDER (2,333 )

ORDER (4,334 )
ORDER (5,334 )
ORDER (6,334 )

ORDER (7,335 )
ORDER (8,335 )
ORDER (9,335 )


CUSTOMER(336,009530650)
CUSTOMER(337,009530650)

ORDER (10,336 )
ORDER (11,336 )
ORDER (12,336 )

ORDER (13,337 )
ORDER (14,337 )
ORDER (15,337 )

333,334,335是同一客户的三行,因为它们具有相同的customer_id,我只想保留最后一个客户(335),并且使334和333的所有订单都参考333。

我的最终数据应该是:

CUSTOMER(335,040535743)
ORDER (1,335)
ORDER (2,335)

ORDER (4,335)
ORDER (5,335)
ORDER (6,335)

ORDER (7,335 )
ORDER (8,335 )
ORDER (9,335 )



CUSTOMER(337,009530650)

ORDER (10,337)
ORDER (11,337)
ORDER (12,337)

ORDER (13,337 )
ORDER (14,337 )
ORDER (15,337 )

如何为所有重复客户执行查询? (我有一个查询返回有重复的customer_id列表)

2 个答案:

答案 0 :(得分:2)

更新Order表:

UPDATE o
SET o.person_id = cc.max_person_id
FROM
    [Order] AS o
  JOIN
    Customer AS c
        ON c.person_id = o.person_id
  JOIN
    ( SELECT customer_id
           , MAX(person_id) AS max_person_id
      FROM Customer
      GROUP BY customer_id
    ) AS cc
        ON cc.customer_id = c.customer_id ;

然后,更新Customer表:

UPDATE c
SET c.person_id = cc.max_person_id
FROM
    Customer AS c
  JOIN
    ( SELECT customer_id
           , MAX(person_id) AS max_person_id
      FROM Customer
      GROUP BY customer_id
    ) AS cc
        ON cc.customer_id = c.customer_id ;

之后,最好将Customer(person_id)定义为PRIMARY KEYUNIQUE约束。

FOREIGN KEYOrder(person_id)

Customer(person_id)约束

答案 1 :(得分:-2)

从这开始:

select person_id from customer where customer_id = 040535743

然后像:

select * from orders where person id in ( select person_id from customer where customer_id = 040535743 )

然后你可以在结果中使用MAX person_id。