Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have 1 Million records in my table.
I want to compare one field called "name" for 1st row to same field of other row and in case of match update a column "parent" with that name . How do i do..?

Example :

Name code area Parent
------------------------------------
Nokia 610 cube
Investments 240 ahnc
Investments 241 hnnc
apple 500 sumo
Iphone 210 robert

so when Name = Name , then update Parent with that name

in this case :

Name code area Parent
------------------------------------
Nokia 610 cube
Investments 240 ahnc Investments
Investments 241 hnnc Investments
apple 500 sumo
Iphone 210 robert
Posted
Comments
jaket-cp 19-Jan-16 5:23am    
It should be possible to do it with an update on a recursive cte.
Or an update on lead/lag - not sure on this one.
But a simulated lead/lag on an join update should work.

In all the above possibilities the ordering of the records need to be defined.
Can you define the ordering of the records.

Also in your example, shouldn't
Investments 240 ahnc Investments
Investments 241 hnnc Investments
be
Investments 240 ahnc
Investments 241 hnnc Investments

As if comparing previous with current,
Investments (240) would be compared with Nokia (610) - no match so Parent would be empty.

Please clarify these points.
Thanks

Break the query up into concepts:

You only need to look at values that exist at least (or only?) twice. So you need to group-by and count them.


Then, all those with a count > 1 (you may need to use a HAVING clause) can be returned in a subquery for an UPDATE Statement setting parent=name

Now, refine this: to save processing time, you may find it useful to reduce the list for the UPDATE by checking to make sure at least one in each set has not already been attended to. This could be handled with a WHERE clause in your UPDATE.



 
Share this answer
 
following is the required query for you.

update TableName set parent = name where  name in (	SELECT NAME FROM TableName GROUP BY Name having count(NAME) > 1)


Hope this will helps you.
 
Share this answer
 
Hi,

Hope this query will get the result. Since update is involved please try this with sample development data before testing with real records. If you are using 2012 please try with Lead Functions which will get rid of this CTE.

SQL
create table #tempDha(Id varchar(50),Code int,Parent varchar(50))

insert into #tempDha values('Nokia',610, 'cube')
,('Invest' ,240 ,'ahnc')
,('Invest', 241 ,'hnnc')
,('apple' ,500 ,'sumo')
,('Iphone', 210 ,'robert' )


with cte as
(
select dense_rank()over(PARTITION by Id order by code) as Ra,* from #tempDha)
update t set t.Parent= t.Parent + ' '+ t.Id
from #tempDha t join cte c on t.Id=c.Id
where Ra>1

Thanks
Dharani
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900