Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have one table, in which I want to update the value for a particular column. The column value should be row number. Based on a column distinct value I need to update the newly created column value.

Example:
Table name - INFO_T

Name | id
------------
Arijit | 120
Suman | 121
Priyam | 122
Arijit | 123
Suvayu | 124
Priyam | 125

I need to add one more column within the above table INFO_T, below is the example. I have to use ROW_NUMBER() OVER function, through which I have to update that SEQ_NO column.

Name | id | Seq_no
------------------
Arijit | 120 |
Suman | 121 |
Priyam | 122 |
Arijit | 123 |
Suvayu | 124 |
Priyam | 125 |

After UPDATE the table, it should be like below -

Name | id | Seq_no
------------------
Arijit | 120 | 1
Suman | 121 | 2
Priyam | 122 | 3
Arijit | 123 | 1
Suvayu | 124 | 4
Priyam | 125 | 3

I am unable to get that above result. Please help me.

Advanced thanks.
Posted
Updated 20-Jul-15 5:28am
v5
Comments
Wendelius 20-Jul-15 11:06am    
First of all, how the data should be sorted? Why Arijit has sequence number 1 and Suvayu has 4?
Also where did the 456 disappear from Arijit and 300 from Priyam?
Bittu14 20-Jul-15 11:13am    
Data has stored based on nothing. Means first I have insert arijit, suman like this. If you can insert the data like arijit, priyam, suvayu, that doesn't matter.
id is primary key. i have entered worng data. sorry for that. i have changed it. check once.
Wendelius 20-Jul-15 11:21am    
Still doesn't make sense. If Id is the primary key, why Arijit has the value 120 two times? Suman has 121 while that value didn't exist in the original data...

How do you calculate the new Id's?
Bittu14 20-Jul-15 11:29am    
I have used some constraints over there, so that it has stared from 120.
Yes i understand. Once again sorry.
Wendelius 20-Jul-15 11:33am    
Okay, new the Id's make sense. How do you calculate the sequence number? For example Priyam why he/she is having 3 while Suvayu has 4. What's the logic behind the number?

Not sure if I still understood the question correctly, but does this kind of query return the correct results?
SQL
create table test21  (
   name varchar(100),
   id   int
);

insert into test21 values ('Arijit', 120);
insert into test21 values ('Suman', 121);
insert into test21 values ('Priyam', 122);
insert into test21 values ('Arijit', 123);
insert into test21 values ('Suvayu', 124);
insert into test21 values ('Priyam', 125);

select t1.name,
       t1.id,
	   t2.seq_no
from test21 t1,
     (select t3.name,  row_number() over (order by t3.name) as seq_no
	  from (select distinct name 
	        from test21) t3
	  ) t2
where t2.name = t1.name
order by t1.id;
 
Share this answer
 
Comments
Bittu14 20-Jul-15 14:29pm    
I want the update code. When I have tried this then some error occurred.
Wendelius 20-Jul-15 14:35pm    
First you should work out the select to run. After that it should be quite simple to modify it to update

What is the error you got?
Bittu14 20-Jul-15 16:06pm    
update test21 set SEQ_NUM =(select
t2.seq_no
from test21 t1,
(select t3.name, row_number() over (order by t3.name) as seq_no
from (select distinct name
from test21) t3
) t2
where t2.name = t1.name
order by t1.id)


I've tried the above query but now I am getting the below error

"ORA-00907: missing right parenthesis" can you please suggest what I am doing wrong here?
Bittu14 20-Jul-15 16:10pm    
select
t2.seq_no
from test21 t1,
(select t3.name, row_number() over (order by t3.name) as seq_no
from (select distinct name
from test21) t3
) t2
where t2.name = t1.name
order by t1.id

this query has given the exact answer but when ever i have tried to update then only i have faced the problem. Please help me out..
Wendelius 21-Jul-15 1:03am    
At least one problem is that the sub-query is fetching values from all rows at the same time. This causes an error since a column can only have a single value. In order to correct this you need a correlation between the row to be updated and the data fetched.

Taken that the select query is correct, try something like:
UPDATE test21
SET SEQ_NUM = a.seq_no
FROM (SELECT t1.id, t2.seq_no
FROM test21 t1,
(SELECT t3.name, ROW_NUMBER() OVER (ORDER BY t3.name) AS seq_no
FROM (SELECT DISTINCT name
FROM test21) t3
) t2
WHERE t2.name = t1.name ) a
WHERE a.id = test21.id;
Here is a query to get the results in the order you have specified.
And also update the variable table @INFO_T with the desired result.
From the CTE format, you should be able to see how the data is being manipulated.
SQL
--setup test data
declare @INFO_T table (Id int, Name varchar(100), Seq_no int);
insert into @INFO_T (Name, Id)
	select 
		'Arijit' Name,
		120 Id
	union select 'Suman', 121
	union select 'Priyam', 122
	union select 'Arijit', 123
	union select 'Suvayu', 124
	union select 'Priyam', 125
;

--update OrigData with FinalData
with GroupNames as ( --Group the Names
	select 
	Name,
	Id,
	row_number() over (partition by name order by id) as rowid
	from @INFO_T OrigData
), NameSequence as ( --Get Squence of Names via Id
	select 
		Name,
		row_number() over (order by id) as Seq_No
	from GroupNames
	where rowid = 1
)
update FinalData
set FinalData.Seq_No = NameSequence.Seq_No
from @INFO_T OrigData
inner join NameSequence
	on OrigData.name = NameSequence.name
inner join @INFO_T FinalData
	on OrigData.Id = FinalData.Id
;

--final result
select Name, Id, Seq_no 
from @INFO_T 
order by Id;


Hope that helps out.
 
Share this answer
 
v2

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