我创建了2个表格如下
表ForgeRock
:
id status amount Name date
1 0 4500 ram 04/02/2012
2 0 2000 shyam 05/09/2013
4 0 1500 ghanshyam 08/06/2015
表ForgeRock1
:
id status amount Name date
3 0 4500 gopal 04/02/2012
2 0 8000 radheshyam 15/11/2013
4 1 1500 ghanshyam 08/06/2015
SQL查询
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock f1
inner join ForgeRock1 f2 on f1.id = f2.id and
f1.Name=f2.Name
union all
Select id,
Status,
amount,
Name,
date
from ForgeRock
where id not in (select distinct id from
ForgeRock1)
union all
Select id,
Status,
amount,
Name,
date
from ForgeRock1
where id not in (select distinct id from
ForgeRock)
union all
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock f1
inner join ForgeRock1 f2 on f1.id = f2.id and
(f1.Name!=f2.Name and f1.date!=f2.date)
union all
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock1 f1
inner join ForgeRock f2 on f1.id = f2.id and
(f1.Name!=f2.Name and f1.date!=f2.date)
结果:
id Status rank Name date
4 1 1500 ghanshyam 08/06/2015
1 0 4500 ram 04/02/2012
3 0 4500 gopal 04/02/2012
2 0 8000 shyam 15/11/2013
2 0 2000 radheshyam 05/09/2013
现在我想将重复记录ID更新为新ID (即上面的结果2是Duplicate,所以将一个记录id从2改为21)
答案 0 :(得分:0)
在 SQL-Server 中,您可以尝试使用ROW_NUMBER()
获取重复项,并将其替换为Common Table Expression
,如下所示:
WITH cte1 AS (
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock f1
inner join ForgeRock1 f2 on f1.id = f2.id and
f1.Name=f2.Name
union all
Select id,
Status,
amount,
Name,
date
from ForgeRock
where id not in (select distinct id from
ForgeRock1)
union all
Select id,
Status,
amount,
Name,
date
from ForgeRock1
where id not in (select distinct id from
ForgeRock)
union all
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock f1
inner join ForgeRock1 f2 on f1.id = f2.id and
(f1.Name!=f2.Name and f1.date!=f2.date)
union all
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock1 f1
inner join ForgeRock f2 on f1.id = f2.id and
(f1.Name!=f2.Name and f1.date!=f2.date)
), cte2 AS (
SELECT id, [status], rank, name, [date], RN
FROM(
SELECT id, [status], rank, name, [date], ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Id) RN
from (
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock f1
inner join ForgeRock1 f2 on f1.id = f2.id and
f1.Name=f2.Name
union all
Select id,
Status,
amount,
Name,
date
from ForgeRock
where id not in (select distinct id from
ForgeRock1)
union all
Select id,
Status,
amount,
Name,
date
from ForgeRock1
where id not in (select distinct id from
ForgeRock)
union all
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock f1
inner join ForgeRock1 f2 on f1.id = f2.id and
(f1.Name!=f2.Name and f1.date!=f2.date)
union all
Select f1.id,
case when f1.status = 1 then f1.status else
f2.status end as Status,
case when f1.status = 1 then f1.amount else
f2.amount end as rank,
f1.Name,
case when f1.status = 1 then f1.date else
f2.date end as date
from ForgeRock1 f1
inner join ForgeRock f2 on f1.id = f2.id and
(f1.Name!=f2.Name and f1.date!=f2.date) )x
) x2
WHERE RN > 1
)
SELECT CASE WHEN cte1.Id = cte2.Id THEN REPLACE(cte1.Id, cte2.Id, CAST(cte1.Id AS NVARCHAR(20)) + '1') ELSE cte1.Id END AS Id, cte1.[Status], cte1.[rank], cte1.name, cte1.[date]
FROM cte1
LEFT JOIN cte2 ON cte1.[rank] = cte2.[rank] AND cte1.name = cte2.name
输出:
Id Status rank name date
4 1 1500 ghanshyam 2015-08-06
1 0 4500 ram 2012-04-02
3 0 4500 gopal 2012-04-02
2 0 8000 shyam 2013-09-11
21 0 2000 radheshyam 2013-05-09