473,493 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I limit a query to 2 of the same value in one col ordered by another col?

3 New Member
Say I have a table:
name game score
Steve Chess 50
Steve Soccer 48
Steve Football 45
Evan Chess 46
Evan Soccer 47
Evan Football 49

I want to return each column for the two highest scores for each person. Can anyone tell me how to do this?
Jul 28 '09 #1
5 3305
rski
700 Recognized Expert Contributor
Gosh my English sucks, can you explain what do you want to achieve?
Wat do you want the query select from that example you give?
Jul 28 '09 #2
PaxMagister
3 New Member
sorry

I want to return

name game score
steve chess 50
steve soccer 48
evan football 49
evan soccer 47
Jul 28 '09 #3
rski
700 Recognized Expert Contributor
If there are only 3 games you can use that simple query, imagine the table name is test
Expand|Select|Wrap|Line Numbers
  1. select * from test where (name,score) not in (select name,min(score) from test group by name);
  2.  
The more complex solution that do not depend on number of games is
Expand|Select|Wrap|Line Numbers
  1. select * from test where (name,score) in (select name,max(score) from test group by name) or (name,score) in (select name,max(score) from test where (name,score) not in (select name,max(score) from test group by name) group by name);
  2.  
I don't know what postgres version you use but to simplify the second query you can use 'with' statement
Jul 29 '09 #4
PaxMagister
3 New Member
Thanks for the response. I guess I should have been a little more vague in my question, or a little more wordy. That was actually a simplified example. What if I wanted to get the top 10, so if there were more games for each person? I guess I can just nest it more, but I was looking for a more flexible solution. Sorry for not being as clear with my first question. Again thanks for responding.
Jul 29 '09 #5
rski
700 Recognized Expert Contributor
You want a simple solution?, I guess writting your own function is not a simple solution is it?
The simplest solution is
1) Upgrade database to 8.4 (unfortuantely it is still non stable engine)
2) Write a query
Expand|Select|Wrap|Line Numbers
  1. select name,game,score from (
  2. select name, game, score, rank() over(partition by name order by score desc) as rank from test)  foo where rank<=2
  3.  
If you want more highest scores you only need to change the condition
Expand|Select|Wrap|Line Numbers
  1. rank<=...
  2.  
Is that simple for you?
Jul 29 '09 #6

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

Similar topics

3
17303
by: Brian Piotrowski | last post by:
Hi All, I'm trying to run a simple query from an ASP page. I want the query to select each individual field in a table and compare it to another table. If the value doesn't exist, I want it...
2
1616
by: troy | last post by:
Hi, I need to retrived a specific row from a query, these these specific row number is the result of another query. Is it possible to do that in one query with a subquery (mysql 4.1.x)? Here...
14
9269
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per...
10
3717
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
0
5746
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
4
3728
by: Yonatan Maman | last post by:
Hi Im using access 2000. and I have a strange problem. when I execute a query on TABLE_A (TABLE_A contains 2 colums: "id" INTEGER and "name" MEMO) Query1: "select id, name from TABLE_A ORDER BY...
2
2610
by: Steve | last post by:
Does any anyone have a procedure for a query where a calculated field returns the previous record's value in another field. For example: A F A Z F M Z The primary key is in random...
9
10806
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
3
1036
by: Laphan | last post by:
Hi All I know that I should supply the DDL for the tables I'm going to talk about, but I'm not 100% on how to generate them just yet. Hopefully my question is more a query methodology question...
22
31145
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
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
7195
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
5453
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
1400
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
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
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.