View Single Post
  #2 (permalink)  
Old 10-10-2006, 07:27 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: Maximum Value - Beginner Question?

[sql]
select e.name, max.total
from
(
select empid,total from
(
select e.empid as "empid", sum(price) as "total"
from hstreatment t,hsvisithistory v,hsemployee e
where t.treatmentid=v.treatmentid and e.empid=v.stylistid
group by e.empid
) tt
where
total=(
select max(total) from
(
select sum(price) as "total"
from hstreatment t,hsvisithistory v,hsemployee e
where t.treatmentid=v.treatmentid and e.empid=v.stylistid
group by e.empid
) tt
)
) max,
hsemployee e
where max.empid=e.empid
[/sql]
As I understand the problem is to find the employee with highest price paid for treatment.
You can give a try to the above, but bear in mind it was not tested on Oracle so you may have to tweak the syntax a bit.
Note that I changed the grouping criteria to employee id (instead of employee name) as this seems natural to me. Beacause of this change we need the outermost select to retrieve the employee name based on the desired id.

Please, do not fire the most expensive employee!

Please, let me know whether the above worked or not.
Reply With Quote