我有一个表客户,其中我有6个属性
CustomerID CustomerName CustomerCity CustomerState DuplicateCustIND
1 Richard Los Angeles New York
2 Jerome Houston Texas
3 Paul Los Angeles New York
在上表中,Richard和Paul有两个重复的列值CustomerCity和CustomerState相同。在检查两个相似的列条件后,我想用Richard的客户ID更新保留列DuplicateCustIND。
直到现在我使用相关查询
Update Customer C1 set C1.DuplicateCustIND = (select CustomerID from customers
where C3.CustomerCity =
C1.CustomerCity and
C3.CustomerState =
C1.CustomerState)
where exists(
Select 1 from Customers C2 where
C2.CustomerCity = C1.CustomerCity and
C2.CustomerState = C1.CustomerState and
C2.RowID<C1.RowID);
以上查询对我没有帮助。
答案 0 :(得分:1)
以下查询可行。
merge INTO customers tgt USING
(SELECT a.id,
a.cname,
a.ccity,
a.cstate
FROM customers a,
(SELECT ccity,
cstate,
COUNT(*)
FROM customers b
GROUP BY ccity,
cstate
HAVING COUNT(*) > 1
) b
WHERE a.ccity = b.ccity
AND a.cstate = b.cstate
) src ON (tgt.id = src.id)
WHEN matched THEN
UPDATE SET tgt.dcustid = src.id;
答案 1 :(得分:0)
您可能依赖customerid
并使用MERGE
语句更新所有大于customerid
城市和州的记录。
MERGE INTO customers t USING
( SELECT MIN(c1.CUSTOMERID) prim_id,
c2.CUSTOMERID sec_id
FROM Customers c1
JOIN customers c2 ON C2.CustomerCity = C1.CustomerCity
AND C2.CustomerState = C1.CustomerState
AND C2.CustomerID > C1.CustomerID
GROUP BY C1.CustomerCity,
C1.CustomerState,
c2.CUSTOMERID ) s ON (s.sec_id = t.CustomerID )
WHEN MATCHED THEN
UPDATE
SET t.DuplicateCustIND = s.prim_id;