Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I use this query it will show result

SQL
select max(id),rno
from y
group by rno


MAX(ID)                       RNO

  3                               30
  4                               40
  1                               10
  6                               60


If I do not use GROUP BY then error occurs

SQL
select max(id),rno from y;
select max(id),rno from y


ERROR at line 1:<br />
ORA-00937: not a single-group group function


But I want result set as below(maximum of id)
 MAX(ID)
  6
 RNO
30
40
10
60


What sh'd I do???
Posted
Updated 25-May-13 8:21am
v2

1 solution

First of all, you need to undesrtand that MAX (aggregate function) returns maximum value as a single result row based on groups of rows, rather than on single rows. Of course, you can always get single row based on single column:
SQL
SELECT MAX(ID)
FROM y

but your query returns max id for each rno.

To get expected output, try this:
SQL
SELECT MAX(ID) AS [Values]
FROM y
UNION ALL
SELECT rno AS [Values]
FROM y

but you'll never know which value belongs to which column (id or rno).
To get description of values, try this:
SQL
SELECT 'max id' AS [Description], MAX(ID) AS [Values]
FROM y
UNION ALL
SELECT  'rno' AS [Description], rno AS [Values]
FROM y


Please, refer these:
group by (functions)[^]
aggregate functions[^]
UNION[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900