473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

creating a row number within an Acess SQL query

I've seen this asked several times, but never a straight answer.
I have a query returning a set of sorted rows. Let's say I get back:

Alice
Bob
Charlie

I want to add a number ranking, so it becomes:

1 | Alice
2 | Bob
3 | Charlie

Is there some way to place a variable within the SQL statement that
increments by 1 for each row?

Mar 29 '06 #1
9 123636
bl*****@carolina.rr.com wrote in message
<11**********************@j33g2000cwa.googlegroups .com> :
I've seen this asked several times, but never a straight answer.
I have a query returning a set of sorted rows. Let's say I get back:

Alice
Bob
Charlie

I want to add a number ranking, so it becomes:

1 | Alice
2 | Bob
3 | Charlie

Is there some way to place a variable within the SQL statement that
increments by 1 for each row?


Something like this?

select
(select count(s.name)
from mytable s
where s.name < = t.name) as mycount,
t.name
from mytable t
order by t.name

This will probably be a drain on recourses (if it works), if there are
lot of rows, and the field to sort on/set criteria on, needs to be
unique.

--
Roy-Vidar
Mar 29 '06 #2
It's a small number of rows coming in the query, and uniqueness doesn't
need to be enforced in this instance.
Your query ALMOST worked. It did give a number, but not in the right
order or at the right time. Explanation:

If my original data set I queried was set up like:
Charlie
Alice
Bob

Then your query returned this:
2 | Alice
3 | Bob
1 | Charlie

It used the original position, not the position after sorting.
I can try passing my query results through another query that does
nothing but add your number to it, but I'd like something a little
cleaner, a "one shot" kind of a deal.

Mar 29 '06 #3
bl*****@carolina.rr.com wrote:
I've seen this asked several times, but never a straight answer.
I have a query returning a set of sorted rows. Let's say I get back:

Alice
Bob
Charlie

I want to add a number ranking, so it becomes:

1 | Alice
2 | Bob
3 | Charlie

Is there some way to place a variable within the SQL statement that
increments by 1 for each row?


Try this thread:

http://groups.google.com/group/comp....b73e9bfb888698

James A. Fortune
CD********@FortuneJames.com

Mar 29 '06 #4
bl*****@carolina.rr.com wrote in message
<11*********************@g10g2000cwb.googlegroups. com> :
It's a small number of rows coming in the query, and uniqueness doesn't
need to be enforced in this instance.
Your query ALMOST worked. It did give a number, but not in the right
order or at the right time. Explanation:

If my original data set I queried was set up like:
Charlie
Alice
Bob

Then your query returned this:
2 | Alice
3 | Bob
1 | Charlie

It used the original position, not the position after sorting.
I can try passing my query results through another query that does
nothing but add your number to it, but I'd like something a little
cleaner, a "one shot" kind of a deal.


Strange ...

When I stuffed a table only containing one field (name) with Alice,
Bob,
and Charlie, then run the excact query I gave you, it resulted in
exactly what you asked for in the initial post.

As you can see by the SQL, for this to work, the field in question will
need to be
1 - unique
2 - be the field on which you sort
3 - be the field used in the comparision in the subquery

For more info, try searching on "ranking query", here are some links
(watch for linebreaks)

http://support.microsoft.com/?kbid=208946
http://msdn.microsoft.com/library/de...tml/sa01j1.asp

--
Roy-Vidar
Mar 29 '06 #5
The normal way is to add an AutoNumber field to your table. Then Access
will create a sequential number for you.
But if you you run a query on that field, you might get
12 Bob
13 Charlie
14 Alice
So the auto-number depends on the order in which you created the records.
On Wed, 29 Mar 2006 20:40:53 +0200, <bl*****@carolina.rr.com> wrote:
I've seen this asked several times, but never a straight answer.
I have a query returning a set of sorted rows. Let's say I get back:

Alice
Bob
Charlie

I want to add a number ranking, so it becomes:

1 | Alice
2 | Bob
3 | Charlie

Is there some way to place a variable within the SQL statement that
increments by 1 for each row?


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Mar 29 '06 #6
Probably because without any SORT BY clause in the query, Access will
natively sort by the table's primary key, which in the OP's post I'd
guess is an autonumber field.

Just a guess.

Much warmth,

planetthoughtful

---
"Lost in thought"
http://www.planetthoughtful.org

Mar 30 '06 #7
All things are possbile. This is.
Some things are unnecessary. This is.
Some things are foolish. This is.

Mar 30 '06 #8
planetthoughtful wrote in message
<11*********************@t31g2000cwb.googlegroups. com> :
Probably because without any SORT BY clause in the query, Access will
natively sort by the table's primary key, which in the OP's post I'd
guess is an autonumber field.


As I've understood, an ORDER BY clause, is not necessary for an
ordinary
ranking query to work, I like to add it though, to present the result
in
the same order as the ranking, but again, I don't think it is entirely
necessary. If you look again, you should be able to find an ORDER BY
clause as the last clause of my suggested SQL statement.

Using Count(*), will usually also do, in stead of Count(FieldName), I
don't know why I keep using the latter version.

But I do think you need a sortable and unique *field*, on which to set
the criterion in the subquery. If the field isn't unique, equal
values/duplicates will get equal rank.

For more samples/explanations, check out for instance
http://support.microsoft.com/kb/q182568/
http://www.fabalou.com/Access/Querie...TotalQuery.asp
http://ourworld.compuserve.com/homep...ip.htm#AUTONUM

If I've competely misunderstood the concept of "Ranking queries" or
"Running totals", I'm happy to learn new ways (except usage of Domain
Aggregates, that is).

--
Roy-Vidar
Mar 30 '06 #9
Lyle Fairfield wrote:
All things are possbile. This is.
Some things are unnecessary. This is.
Some things are foolish. This is.


This reminds me of the time an itinerant street preacher came to
Oakland U.

That preacher called Becky a whore. That wasn't nice. Of course,
everybody on campus knows he was right; but it still wasn't nice. --
Ken Harrington

Putting a row number on an Access query does fly 180 degrees opposite
that flown by database theory. Maybe this will be the simplest way to
solve some future problem. Maybe not. The future's not ours to see
:-).

James A. Fortune
CD********@FortuneJames.com

Mar 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1571
by: Ed Hawkes | last post by:
I am having the following problem and any help would be GREATLY appreciated: In an application I am developing, at some points we create a new table. When I create this table on another users...
3
14111
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
14
2443
by: Crimsonwingz | last post by:
Need to calculate a sum based on a number of factors over a period of years. I can use formula ^x for some of it, but need totals to carry over in the sum and have only been able to do this thus...
0
7896
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
5
9402
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
2
1546
by: Dave Monk | last post by:
Hi, I'm reasonably proficient in PHP but have been asked how to do something which has got me stumped. Hence, my posting: I want to create a small number of variables, $pos1, $pos2 ... $pos5...
1
1315
by: fabdulla | last post by:
I have a varible string that changes data everytime, but there is a number within the string that I am interested in. For example the string would like something like this: U1sootblowerIK23:bypass,...
3
2103
by: DrDarwin | last post by:
I would like to use the 'grand total' value of a pivot table generated from (or within) a query in an equation. I am unfamiliar with how to call upon that value using VBA or SQL (through VBA). It...
2
1518
by: Dave Smith | last post by:
I’m trying to run a query from within a query "But Run it Last". Basically I’m wondering if I can run a query or sql in a order so that all filters and formulas in a query will run, but before...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.