Click here to Skip to main content
14,920,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table:
SQL
tbl_1 :
 
TypeID    Product        
 
9        ELectronic                 
8         Health                                                
5        Cloth            
2        Health                                                
1        Cloth            

Now I want to add a column which shows total count in sequence order as:
SQL
TypeID    Product       Total  
 
9        ELectronic      1            
8         Health         2                                       
5        Cloth           3 
2        Health          4                                      
1        Cloth           5


Above table has 5 row, so it add count in sequence order(1-5, as per row count) in another column (as Total).

Any suggetion really appreciate !
Posted
Updated 16-Apr-13 20:23pm
v2

1 solution

SQL
select TypeID,Product,Row_Number() over(order by TypeId desc) as Total 
from TblNm

Happy Coding!
:)
   
v2
Comments
Mas11 17-Apr-13 2:39am
   
Hi, Aarti you are genious in SQL, You help me lots of time in SQL. Thanks CodeProject for providing such wonderfull blog ! :)
Aarti Meswania 17-Apr-13 2:42am
   
It was easy... anyways thanks :)
Glad to help you! :)
Mas11 17-Apr-13 2:46am
   
Ya, but I haven't an idea regarding this. For this I was using loop for count row, then bind to that column(lengthy process). SO I'm posting here!
Aarti Meswania 17-Apr-13 2:51am
   
:)
It happens because you was not aware of Row_number function
but now you know better solution then that lengthy process

some other function to numbering rows in sql... like Dense_rank, rank etc you can study them also

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