Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello all good moring


I have a problem to swaping roll no with serial No.
I have a Table like


id roll_No name
----------------------------------
1 1 raj
2 3 jogindar
3 6 yograj
4 7 amit
5 8 Upkar
6 10 hemraj
8 21 khemraj


and I want the result of like this
=======================================================

id roll_No aname
-----------------------------------
1 1 amit
4 2 amit
6 3 hemraj
2 4 jogindar
8 5 khemraj
5 6 Upkar
3 7 yograj

=-=============================
UPDATE Customer SET Name=Address , Address=Name
^
this query is working properly but

update tbl_testing set roll=(select row_number() over (order by aname))

this query is not usable






please help me its urgent

thanks in advance
Posted
Updated 10-Sep-13 19:51pm
v6
Comments
ArunRajendra 11-Sep-13 0:28am    
do you want roll number assigned in alphabetical order. Right?
Hem Raj Thakur 11-Sep-13 0:31am    
yes sir
[no name] 11-Sep-13 1:30am    
why can't u use order by name..

Try this....
-- Same as OriginalGriff Solution but u have to add "PARTITION BY"
SQL
UPDATE MyTable SET RollNo=j.RowNo
FROM MyTable INNER JOIN
   (SELECT Id, ROW_NUMBER() OVER (PARTITION BY [Name] ORDER BY [Name]) AS RowNo FROM MyTable) as j
   ON MyTable.Id = j.Id


ROW_NUMBER (Transact-SQL)[^]
How to use ROW_NUMBER() to enumerate and partition records in SQL Server[^]

Regards,
GVPrabu
 
Share this answer
 
Comments
Maciej Los 11-Sep-13 17:52pm    
+5!
gvprabu 12-Sep-13 3:55am    
thanks my frnd.... :-)
This is a little complicated, but it's doable:
SQL
UPDATE MyTable SET RollNo=j.RowNo
FROM MyTable INNER JOIN
   (SELECT Id, ROW_NUMBER() OVER (ORDER BY [Name]) AS RowNo FROM MyTable) as j
   ON MyTable.Id = j.Id

When you
SQL
SELECT * FROM MyTable
after that, you will partly get what you want: The rows will not be ordered, but the Roll_no will be as you wanted.
To return them in the exact order you want, you need to specify a sort order - because SQL can return rows in any order convenient to it if you don't:
SQL
SELECT * FROM MyTable ORDER BY RollNo
 
Share this answer
 
Comments
Hem Raj Thakur 11-Sep-13 3:53am    
thank you sir this query is helpfull to me.
OriginalGriff 11-Sep-13 4:19am    
You're welcome!
Hem Raj Thakur 11-Sep-13 4:55am    
sir one more question?
id roll_No aname class section
------------------------------------------
1 1 amit I A
4 2 amit I B
6 3 hemraj I A
2 4 jogindar I B
8 5 khemraj I B
5 6 Upkar I B
3 7 yograj I A
-----------------------------------------
I want to
id roll_No aname class section
------------------------------------------
1 1 amit I A
4 1 amit I B
6 2 hemraj I A
2 2 jogindar I B
8 3 khemraj I B
5 4 Upkar I B
3 3 yograj I A
----------------------------------------
please solve this thank you.

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