p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   relative size query (http://p2p.wrox.com/showthread.php?t=36009)

noam December 11th, 2005 02:25 PM

relative size query
I want to buid a query that will show me the relative size of data in a table. for example i have a table with names and age:

noam : 27
david : 34
gili : 24
shelly 34

the query should give me :
david : 1
shelly : 1
noam : 2
gili : 3

i think it needs a subquery on the select field but i dont know ...
thanks noam ..

echovue December 11th, 2005 03:47 PM

I think you should be able to do it with the following...

Add the name field to your query twice. Then hit the Group By button at the top of the Query Window (Its the thing that looks kind of like an 'E'.

On the second Name, click the drop down on Group By, and choose Count.

Yell if you need more info on this one,



noam December 11th, 2005 07:09 PM

sorry but that dosent give me what i wanted , you did not refer to the age of the person at all ...
i need to do a ranking between all the persons by age , take a look again at my request please....

mmcdonal December 12th, 2005 08:41 AM

Actually, you asked to show the relative "size of data", not the descending order of each person by age ("i need to do a ranking between all the persons by age.")

However, that would look like:


But would not look like:

David 1
Shelly 1
Noam 2
Gili 3

In fact, it would look like this in a report where you added a number column (you wouldn't necessariy add this in the query results):

David 1
Shelly 2
Noam 3
Gili 4

I am thinking the only way to add this would be through code. You would have to do a query just on the age field first, and then select Unique Values = Yes. Order By Age DESC. Then you could add a number column to rank them. I am not sure how to do this other than to create a table called tblAgeRank with two fields, Age and Rank.

Then do a DELETE query on the table, then do an APPEND query, which would be the query in the last paragraph, and put the rank in with that query.

Then do a thrid query that matches the original table data (Name field, Age field) against tblAgeRank Age field, and then add the Rank field to your query.

Kind of the long way around. I could code this. I am sure another poster has a more elegant solution.



kindler December 12th, 2005 06:00 PM

What you're attempting should probably be done using VB and DAO RecordSets to parse the sorted (descending) age table, and create a new table based on those results. You shouldn't need to add a new column to the original table, just some code like:

Set rst 'blah blah blah
If Not rst.BOF And Not rst.EOF
colName = rst.Name
colAge = rst.Age
rank = 1
Do Until rst.EOF
If colAge = rst.Age Then
'Add colName, Rank to new table
'Add colName, Rank to new table
Rank = Rank + 1
End If
'Add colName, Rank (not sure about this, figure out if necessary to avoid off-by-one error

All times are GMT -4. The time now is 12:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.