Solved

Make row counter in query

Posted on 2001-09-03
11
455 Views
Last Modified: 2008-02-01
Hello!

How can i do row counter in query like that.

select id from table1
and output will be:

counter  id
  1       4
  2       9
  3       20
  4       21
  5       50
 ........

thank you!
0
Comment
Question by:kaspiton
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 1

Expert Comment

by:george74
ID: 6450236
hi,

there is one way you could do this, but it requires you to deal with another table. The following example creates your table one (that you already have), than selects it's values into a new temporary table together with an identity column (used for rowcount).
Hope this helps.

Cheers,
George

set nocount on
create table table1(id int)
declare @i int
set @i = 10
while @i > 0 begin
     insert into table1 select @i
     set @i  = @i - 1
end

select identity(int, 1, 1) as rix, * into #table2 from table1         -- this is the line you need for having the incremented values
select * from #table2

drop table table1
drop table #table2
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6450316
select counter = (count(*) from table1 t1 where t1.id < table1.id) + 1 ,
id
from table1
order by id
0
 

Author Comment

by:kaspiton
ID: 6450325
Can i do this without sub queries?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 70 total points
ID: 6450522
I found an article that may interest you: "INF: How to Dynamically Number Rows in a Select Statement"
at http://support.microsoft.com/support/kb/articles/q186/1/33.asp 
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6450609
Don't see how.
You need to get the data to display from the table and also the number of records in front of it - don't see how you can get that without another copy of the table.

Can join the table to intself but that's not much different from the subquery.
0
 
LVL 3

Expert Comment

by:ibro
ID: 6450673
The method nigel suggests will be very slow for a big queries. By big I mean queries that involve tables with large number of records. However it will be suitable for small result-sets. If you involve tables with a large number of records, better use george74's method with temporary table. It will give you best performance.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6466105
Please maintain:

Questions Asked 24
Last 10 Grades Given A A A A A A A A A A  
Question Grading Record 20 Answers Graded / 20 Answers Received
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6498613
I am sure that my solution is the best.
0
 
LVL 3

Expert Comment

by:ibro
ID: 6499020
that was very modest :-)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6521913
kaspiton, can you comment or close the question ?
0
 

Expert Comment

by:Dafvid
ID: 7332733
The "INF: How to Dynamically Number Rows in a Select Statement" KB entry link is broken. This one is correct http://support.microsoft.com/default.aspx?scid=kb;en-us;Q186133 for future reference.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question