1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

Maximum Value - Beginner Question?

Discussion in 'SQL - Questions and Answers' started by Jim, Oct 10, 2006.

  1. Jim Guest

    Dialect: Oracle
    Hi,

    I'm a massive beginner of SQL and just having trouble finishing this problem. I need to get the Max of SUM(price) and output both this and e.name. I'm just not to sure how to go about it, im guessing I need to use a subquerry.

    SELECT e.name, SUM(price)
    FROM hsTreatment t, hsVisitHistory V, hsEmployee e
    WHERE t.treatmentid = v.treatmentid
    AND e.empid = v.stylistid
    GROUP BY e.name;
  2. Dimitar Guest

    Dialect: SQL 92
    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

    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.
  3. Manithan Guest

    Dialect: MS SQL

Share This Page