0

I have the following table :

  **Country     Name        Number**
   us           John         45
   us           Jeff         35
   fr           Jean         31
   it           Luigi        25
   fr           Maxime       23
   ca           Justin       23

This table is order by Number. I want to have a query that for each country give me the name with highest number :

  **Country     Name        Number**
   us           John         45
   fr           Jean         31
   it           Luigi         25
   ca           Justin        23

I try to use distinct but I can't only make it on country if I want to print the all thing...

Have an idea ?'

EDIT : The table is obtain by a subquery

Danny
  • 79
  • 1
  • 1
  • 6

4 Answers4

1

Try below query:

SELECT Country, MAX(numbeer) FROM Table_Name GROUP BY Country

PFB updated query to include Name:

SELECT t1.* FROM table1 t1 INNER JOIN 
(SELECT country, max(numbeer) as numbeer FROM table1 GROUP BY country) t2
ON t1.country=t2.country AND t1.numbeer=t2.numbeer;
Rajesh
  • 2,135
  • 1
  • 12
  • 14
1

I do not have an Oracle db handy but I got this working in my SQL Server db and am pretty sure it will work in Oracle (meaning I think I am using ANSI sql which should work in most db's):

SELECT m.Country,m.Name,m.number
FROM mytable m
INNER JOIN (
    select country, MAX(number) as number 
    FROM mytable GROUP BY Country
) AS tmp ON m.Country = tmp.Country and m.Number = tmp.number
ORDER BY m.Number DESC

This has the added benefit that it should give you records when you have two people in a given country that have the same number.

You didn't give us a table name so I just called it mytable.

Troy
  • 146
  • 4
  • Thanks for the answer ! Oracle SQL tells me that I have a 'missing keyword' but I can't figure out which one.. – Danny May 06 '15 at 20:31
1

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE Countries AS
          SELECT 'us' AS Country, 'John' AS Name, 45 AS "Number" FROM DUAL
UNION ALL SELECT 'us' AS Country, 'Jeff' AS Name, 35 AS "Number" FROM DUAL
UNION ALL SELECT 'fr' AS Country, 'Jean' AS Name, 31 AS "Number" FROM DUAL
UNION ALL SELECT 'it' AS Country, 'Luigi' AS Name, 25 AS "Number" FROM DUAL
UNION ALL SELECT 'fr' AS Country, 'Maxime' AS Name, 23 AS "Number" FROM DUAL
UNION ALL SELECT 'ca' AS Country, 'Justin' AS Name, 23 AS "Number" FROM DUAL;

Query 1:

SELECT Country,
       MAX( Name ) KEEP ( DENSE_RANK FIRST ORDER BY "Number" DESC ) AS "Name",
       MAX( "Number" ) AS "Number"
FROM   Countries
GROUP BY Country

Results:

| COUNTRY |   Name | Number |
|---------|--------|--------|
|      ca | Justin |     23 |
|      fr |   Jean |     31 |
|      it |  Luigi |     25 |
|      us |   John |     45 |
MT0
  • 143,790
  • 11
  • 59
  • 117
0

Use row_number():

select t.Country, t.Name, t.Number
from (select t.*,
             row_number() over (partition by country order by number desc) as seqnum
      from table t
     ) t
where seqnum = 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786