473,410 Members | 1,952 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,410 software developers and data experts.

Need to rank top 3 WITHIN groups

2
I have a query that I need to show the top 3 within each grouping. I have several distance combinations for Employees and Facilities based on distance between zip codes. I need to get the lowest 3 distances for each employee. (some employees may have only 1 possible record, while others will have more than 5). I have Emp_ID, HomeZip, FacilityZip, Distance --
#11111, Betty, 64015, 64052, 25
#11111, Betty, 64015, 64054, 50
#11111, Betty, 64015, 64057, 75
#11111, Betty, 64015, 65044, 100
#11111, Betty, 64015, 65045, 112
#11111, Betty, 64015, 64050, 120
#22222, Tom, 74015, 74052, 35
#22222, Tom, 74015, 74054, 70
#22222, Tom, 74015, 76057, 75
#22222, Tom, 74015, 75044, 123
#22222, Tom, 74015, 75045, 212
#22222, Tom, 74015, 74050, 320
#22222, Tom, 74015, 48000, 274
... etc.
So I'd get the lowest 3 distances for #11111, Betty and then the lowest 3 for #22222, Tom and so on. Anyone out there able to help me? Thanks!
Dec 17 '07 #1
3 1380
Rabbit
12,516 Expert Mod 8TB
I have a query that I need to show the top 3 within each grouping. I have several distance combinations for Employees and Facilities based on distance between zip codes. I need to get the lowest 3 distances for each employee. (some employees may have only 1 possible record, while others will have more than 5). I have Emp_ID, HomeZip, FacilityZip, Distance --
#11111, Betty, 64015, 64052, 25
#11111, Betty, 64015, 64054, 50
#11111, Betty, 64015, 64057, 75
#11111, Betty, 64015, 65044, 100
#11111, Betty, 64015, 65045, 112
#11111, Betty, 64015, 64050, 120
#22222, Tom, 74015, 74052, 35
#22222, Tom, 74015, 74054, 70
#22222, Tom, 74015, 76057, 75
#22222, Tom, 74015, 75044, 123
#22222, Tom, 74015, 75045, 212
#22222, Tom, 74015, 74050, 320
#22222, Tom, 74015, 48000, 274
... etc.
So I'd get the lowest 3 distances for #11111, Betty and then the lowest 3 for #22222, Tom and so on. Anyone out there able to help me? Thanks!
Use a subquery to return criteria by selecting the top 3 sorting by ascending distance.
Dec 17 '07 #2
svogel
2
How do I do that? When I try to do a Top 3 query, I get the top 3 for all of the records. I want the top 3 for each grouping. How can I do this in either the query design or sql statement? Any sample code you can provide using this data I have would be much appreciated. Thanks!
Dec 18 '07 #3
Rabbit
12,516 Expert Mod 8TB
No no, you use the Top 3 query in a subquery as part of the WHERE criteria.
Dec 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Blanchard | last post by:
I'm new to SQL and databases and have a delema which I'm sure is an easy thing for you experts out there to do. What I have is this: I have a table similliar to this: table name: tablename...
5
by: angelasg | last post by:
I am working with employee schedules. Each schedule is comprised of segments (shift, lunch, break, training, etc.) that have rankings. Each record has the employee id, the date the shift starts,...
4
by: Nip | last post by:
I am trying to make a database for my test participants. I have 10 participants and have a table with them called participants which includes an auto number ID and then the participant number and...
13
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm...
1
by: volunteer | last post by:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="filename.xsl"?> <markers date="20060523"> 04:21:50 PM <marker sn="1" rank="6" name="john" /> <marker sn="2" rank="5"...
5
by: codie | last post by:
I got my code to work be the print out go's down on a angle is there anyway to stop this from happing. P.S sorry for taking up your time class Card: suitList = rankList = ...
2
by: codie | last post by:
i need to sort a list of card like the rank sample: ACE of h, ACE of d, ACE of c, ACE of s, 2, 2, 2, 2 this is my code so can anyone help me??? class Card: suitList = rankList =
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
2
by: RSethi | last post by:
I want to rank sales of a business by retail store within each region,state andcity. I want to store the rank in the column rank. How do I do this? Table structure. region, state, city,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.