Solved

Make row counter in query

Posted on 2001-09-03
11
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

623 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