title: using max() and sum()
body: <p>I need to find the 'name of the branch that has made the most money in 2009'.  My tables are below:</p>
  
  <pre><code>Rental
  (cid, copyid, outdate, returndate, cost)
  
  Copy
  (copyid, mid, bid)
  
  Branch
  (bid, bname, baddress)
  </code></pre>
  
  <p>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.</p>
  
  <p>Output the branch name and sum (I get a summary of all branches with this):</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Output the max of sum (I don't get my branch name with this):</p>
  
  <pre><code>SELECT sum(total_cost)
  FROM (SELECT max(cost) as total_cost FROM rented WHERE outdate between '20090101' and '20091231') x;
  </code></pre>
  
  <p>How can I merge these two together to get only the max sum branch name?</p>