I need to find the 'name of the branch that has made the most money in 2009'. My tables are below:
Rental
(cid, copyid, outdate, returndate, cost)
Copy
(copyid, mid, bid)
Branch
(bid, bname, baddress)
I have written the following code, and it outputs the sum of all branches, but I need the sum of the branch that made the most money. I am not sure how to join a max() and sum() function in the same query. I am using Oracle 2007.
Output the branch name and sum (I get a summary of all branches with this):
SELECT bname, sum(cost) as Total
FROM rented R join copy C on R.copyid = C.copyid join branch B on C.bid = B.bid
WHERE outdate between '20090101' and '20091231'
GROUP BY bname;
Output the max of sum (I don't get my branch name with this):
SELECT sum(total_cost)
FROM (SELECT max(cost) as total_cost FROM rented WHERE outdate between '20090101' and '20091231') x;
How can I merge these two together to get only the max sum branch name?