Home » RDBMS Server » Server Utilities » Problem with Group by
Problem with Group by [message #71355] Tue, 22 October 2002 05:26 Go to next message
Sam
Messages: 255
Registered: April 2000
Senior Member
From the scott/tiger emp table, i want to find out which department pays the total highest salary. i dont seem to get the right SQL. i tried,
select deptno, max(sum(sal)) from emp group by deptno
but it doesn't work. can anyone suggest, please.
thanks, SAM
Re: Problem with Group by [message #71359 is a reply to message #71355] Tue, 22 October 2002 07:23 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
SQL> ed
Wrote file afiedt.buf

  1  select deptno,a from
  2  (
  3     select deptno,sum(sal) a from emp
  4     group by deptno
  5     order by a desc
  6  )
  7* where rownum < 2
SQL> /

    DEPTNO          A
---------- ----------
        30       9400
Re: Problem with Group by [message #71361 is a reply to message #71355] Tue, 22 October 2002 07:43 Go to previous messageGo to next message
B
Messages: 327
Registered: August 1999
Senior Member
not the best way but ...

select deptno, total from ( select deptno,sum(sal) total from emp group by deptno )
where total = ( select max(total ) from ( select deptno,sum(sal) total from emp group by deptno ))

or

1 select deptno, sum(sal) total from emp
2 group by deptno
3 having sum(sal) = ( select max(subtotal) from ( select sum(sal) subtotal from emp group by deptno ))

may be some better ways....
Re: Problem with Group by [message #71383 is a reply to message #71355] Thu, 24 October 2002 12:39 Go to previous messageGo to next message
sai sreenivas jeedigunta
Messages: 370
Registered: November 2001
Senior Member
Select deptno from emp
where sal=(select max(sal) from emp)
U don't understand the question [message #71401 is a reply to message #71383] Mon, 28 October 2002 02:46 Go to previous message
B
Messages: 327
Registered: August 1999
Senior Member
Or it's me :-)
Previous Topic: Move data
Next Topic: SQL*Loader Performance (Multi-file-table Load)
Goto Forum:
  


Current Time: Tue May 14 23:16:05 CDT 2024