Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help combining a GROUP BY clause with a ROWNUM

pmdciDec 18 2013 — edited Dec 19 2013

Hi Folks,

I have a simple query request that I need to perform in my data warehouse

(well I am sure it is simple for you guys, but I am no to this )

The requirement is: Show the TOP 2 theatres by the value of ticket sales.

This information is contained in my fact table: FactTicketPurchase. Here is a sample of the data:

Select * from TicketPurchase:

TICKETPURCHASEIDCLIENTIDPRODUCTIONIDTHEATREIDTIMEIDTROWIDSUMTOTALAMOUNT
6000625276621942407
6000725277221988366
60008252774211001406
60009252776211015376
60010252779211037396
60011252779211038377

So here is the initial query I envisaged (which I believe is correct?)

SELECT TheatreID, SUM(SumtotalAmount) SumTotalAmount

FROM FactTicketPurchase

GROUP BY TheatreID

ORDER BY SumTotalAmount DESC;

The above query returned to me 6 rows (that is, one for each Theatre registered in the fact table):

THEATREIDSUMTOTALAMOUNT
2120908
2519365
2215232
2314022
2413667
2613028

However, since the requirement is to return only the TOP 2 theatres, I would like my query above to only return the top two rows, which are the theatres whose IDs are 21 and 25.

I tried using ROWNUM but that doesn't give the result I expected (and I know why! makes sense ) But I don't know then how to obtain the result I'd like.

Could someone please advise?

Regards,

P.

This post has been answered by Frank Kulash on Dec 18 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 16 2014
Added on Dec 18 2013
21 comments
63,292 views