I created 2 table as follows
Table 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
Table 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 query
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)
Result:
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
Now I want to update duplicate record id to new id (i.e. In above result 2 is Duplicate,so change one record id from 2 to 21)
I got the answer from stackoverflow Query but not working in Sqlite
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
Aucun commentaire:
Enregistrer un commentaire