Solved

Make row counter in query

Posted on 2001-09-03
11
451 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
Comment Utility
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
Comment Utility
select counter = (count(*) from table1 t1 where t1.id < table1.id) + 1 ,
id
from table1
order by id
0
 

Author Comment

by:kaspiton
Comment Utility
Can i do this without sub queries?
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 70 total points
Comment Utility
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
Comment Utility
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
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 3

Expert Comment

by:ibro
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
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 69

Expert Comment

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

Expert Comment

by:ibro
Comment Utility
that was very modest :-)
0
 
LVL 69

Expert Comment

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

Expert Comment

by:Dafvid
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now