3

I have spent the last 2 hours searching for this, and every thing I have tried has not worked. I have a table_Sessions for which I want to return a BranchID and Average cost of each session. But I only want to show the top 2 averages.

I have literally tried everything i have come across. either it doesnt fit my case, or I have appropriated it wrong. Either way, I'm going crazy here.

Here is what I have so far, and it seems to do about 80% of what I want it to (last 20% being only show the top 2 highest average costs)

SELECT BRANCHID, AVG(SESSIONPRICE)
FROM SESSIONS 
GROUP BY BRANCHID
ORDER BY AVG(SESSIONPRICE) DESC;

If someone could PLEASE tell me how to append to it, I would greatly be appreciative of it. I am using Oracle SQL Developer.

Thanks

3
  • What happens if there are more than 2 branchid's sharing the top two avg(sessionprice)? Do you still only want to display 2 rows? Commented Oct 3, 2016 at 10:59
  • Yeah, but I dont have such a case Commented Oct 3, 2016 at 11:01
  • If this is for a homework question, then it's not so much of an issue. If it's for reporting of a production system, then it becomes more important. If you really do only care about the first two rows, and you do have a case where you have 2 (or more) sharing one of the top two average session prices, do you care if the branchids reported by your query could be different from run to run? Because if you do care, then you should introduce additional elements in your order by clause to ensure the ordering is fixed when ties (aka draws) occur. E.g. ORDER BY AVG(SESSIONPRICE) DESC, branchid asc Commented Oct 3, 2016 at 11:24

5 Answers 5

7
SELECT (BRANCHID,AVGPRICE
FROM (SELECT BRANCHID, AVG(SESSIONPRICE) as AVGPRICE
FROM SESSIONS 
GROUP BY BRANCHID
ORDER BY AVG(SESSIONPRICE) DESC)
WHERE rownum <= 2;
Sign up to request clarification or add additional context in comments.

Comments

6

In newer versions of Oracle (12c and above) you can use Fetch First, i.e.:

SELECT BRANCHID, AVG(SESSIONPRICE)
FROM SESSIONS 
GROUP BY BRANCHID
ORDER BY AVG(SESSIONPRICE) DESC
FETCH FIRST 2 ROWS ONLY;

In elder versions you can use rownum. However for this we need to make sure your ordering is evaluated first, so you need to put your existing query inside a sub-query, then add the WHERE clause to that to filter by rownum.

For example:

SELECT * FROM
    (SELECT BRANCHID, AVG(SESSIONPRICE)
    FROM SESSIONS 
    GROUP BY BRANCHID
    ORDER BY AVG(SESSIONPRICE) DESC) myQuery
WHERE rownum <= 2;

See https://www.techonthenet.com/oracle/questions/top_records.php for further explanation of rownum.

1 Comment

YES. The second example: using rownum worked! Thanks a lot. Now that i see it, I shouldve known! Drats. Thanks again!
5

More recent Oracle versions have FETCH FIRST:

SELECT BRANCHID, AVG(SESSIONPRICE)
FROM SESSIONS 
GROUP BY BRANCHID
ORDER BY AVG(SESSIONPRICE) DESC
FETCH FIRST 2 ROWS ONLY

For older Oracle versions, useROWNUM. Probably something like:

select * from
(
    SELECT BRANCHID, AVG(SESSIONPRICE)
    FROM SESSIONS 
    GROUP BY BRANCHID
    ORDER BY AVG(SESSIONPRICE) DESC
) dt
WHERE ROWNUM <= 2

5 Comments

didn't oracle also have rownum? like so: WHERE ROWNUM <= 2
This gives me an "SQL command not ended properly" starting at the row and line of the FETCH FIRST
Seems like your Oracle version is too old for FETCH FIRST. Check out rownum instead.
@jarlh i think you have to include ; in the end
@jarlh I have checked out rownum also, and every time it gives me either an "Group By" or "order by" error
2

Use SQL LIMIT command to limit the number of results or SQL TOP if not using MySQL.

Both do the same thing.

Use SQL LIMIT command to limit the number of results or SQL TOP if not using MySQL.

Both do the same thing.

with TOP

SELECT TOP 2 BRANCHID, AVG(SESSIONPRICE)
FROM SESSIONS 
GROUP BY BRANCHID
ORDER BY AVG(SESSIONPRICE) DESC;

Comments

2

UPDATED

SELECT *
FROM
  (SELECT BRANCHID,
          AVG(SESSIONPRICE)
   FROM SESSIONS
   GROUP BY BRANCHID
   ORDER BY AVG(SESSIONPRICE) DESC FETCH FIRST 2 ROWS ONLY)
WHERE rownum <= 2; 


Reference:

2 Comments

This gives me an "SQL is not properly ended" error. Starting at the line of FETCH FIRST
@RobBor: check this query

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.