Solved

Make row counter in query

Posted on 2001-09-03
11
454 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

861 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