SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 10-10-2006, 04:30 AM
Jim
 
Posts: n/a
Default Oracle dialect question:

Maximum Value - Beginner Question?


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;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

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

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 10-12-2006, 11:33 PM
Manithan
 
Posts: n/a
Default MS SQL answer. Re: Maximum Value - Beginner Question?

U need antivirus?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Moderation Tools:



All times are GMT. The time now is 07:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7