473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 123592
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
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
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
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
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
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.