If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select query that bring maximum two rows for the same record in the field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-08, 14:40
koandy1983 koandy1983 is offline
Registered User
 
Join Date: Jun 2008
Posts: 7
Select query that bring maximum two rows for the same record in the field

Hi there!

I have the next table:

ID NAME Points
1 Name1 4
2 Name1 3
3 Name1 7
4 Name2 9
5 Name2 9
6 Name2 6
7 Name3 2
8 Name3 7
9 Name3 9
10 Name3 1

How to construct the SELECT query to bring maximum two records for the same name?

Like so:

1 Name1 4
2 Name1 3
3 Name2 9
4 Name2 9
5 Name3 2
6 Name3 7
Reply With Quote
  #2 (permalink)  
Old 06-09-08, 14:48
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
How do you decide which two rows should be returned? If there are less than two rows, do you want to return any rows?

-PatP
Reply With Quote
  #3 (permalink)  
Old 06-09-08, 14:51
koandy1983 koandy1983 is offline
Registered User
 
Join Date: Jun 2008
Posts: 7
Let's say I will return first two results with greatest points, like ORDER BY points DESC.
If are less than two rows, return the single row found.
Reply With Quote
  #4 (permalink)  
Old 06-09-08, 16:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
select ID
     , NAME 
     , Points
  from daNextTable as T
 where ( select count(*) 
           from daNextTable  
          where NAME = T.NAME
            and Points > T.Points ) < 2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-10-08, 01:55
koandy1983 koandy1983 is offline
Registered User
 
Join Date: Jun 2008
Posts: 7
This is not quite good, because if number of points are equal for each name, the query will bring all the records.
That table with points was just an example. The points can have random values between 0-10.

All I want is to bring maximum 2 instances of the same name, order by greatest points.
Reply With Quote
  #6 (permalink)  
Old 06-10-08, 05:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
actually, yes, it is quite good

ties are important, and if you ignore ties, your data is misleading

one thing you could do is use my query, and then, if you still think it's the right thing to do, you could ignore ties when printing the results
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On