0

I have table :

table blah:  
| name | nmb |
+------+-----+
| andy | 10  |
| alice|  5  |
| carol|  9  |

select name,MAX(nmb) from blah;

Not a single-group group function,
How to get output like :

| name | nmb |
+------+-----+
| andy | 10  |

Could anyone guide in the right direction?

shA.t
  • 15,232
  • 5
  • 47
  • 95
deckyazmi
  • 3
  • 5
  • @Pang I think it is just a typo andy=andi – Jorge Campos Jun 14 '16 at 23:51
  • Possible duplicate of [How do I limit the number of rows returned by an Oracle query after ordering?](http://stackoverflow.com/questions/470542/how-do-i-limit-the-number-of-rows-returned-by-an-oracle-query-after-ordering) – shA.t Jun 14 '16 at 23:56
  • Possible duplicate of [SQL not a single-group group function](http://stackoverflow.com/questions/1795198/sql-not-a-single-group-group-function) – Florin Ghita Jun 15 '16 at 05:39

2 Answers2

1
SELECT MAX( name ) KEEP ( DENSE_RANK LAST ORDER BY nmb ) AS name,
       MAX( nmb ) AS nmb
FROM   blah

or

SELECT *
FROM   (
  SELECT *
  FROM   blah
  ORDER BY nmb DESC
)
WHERE ROWNUM = 1
MT0
  • 86,097
  • 7
  • 42
  • 90
0

You forget the group by clause

select name, MAX(nmb) from blah group by name;

But it will not give you the result you are expecting. In fact it will return the same result as you have on your table.

table blah:
| name | nmb |
| andy | 10  |
| alice|  5  |
| carol|  9  |

That's because it will group the values by the column name since there are no repeated names it will show you all registries.

If you want to get just the name with bigger nmb you have to add an where clause and a subquery to get only one registry like this:

select name, nmb
  from (select name, MAX(nmb) as nmb
          from blah
         group by name
        order by MAX(nmb) desc) as t
 where rownum = 1
Jorge Campos
  • 20,662
  • 7
  • 51
  • 77