Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let's make some test data:</p> <pre><code>create table client (client_id integer not null primary key auto_increment, name varchar(64)); create table portfolio (portfolio_id integer not null primary key auto_increment, client_id integer references client.id, cash decimal(10,2), stocks decimal(10,2)); insert into client (name) values ('John Doe'), ('Jane Doe'); insert into portfolio (client_id, cash, stocks) values (1, 11.11, 22.22), (1, 10.11, 23.22), (2, 30.30, 40.40), (2, 40.40, 50.50); </code></pre> <p>If you didn't need the portfolio ID, it would be easy:</p> <pre><code>select client_id, name, max(cash + stocks) from client join portfolio using (client_id) group by client_id +-----------+----------+--------------------+ | client_id | name | max(cash + stocks) | +-----------+----------+--------------------+ | 1 | John Doe | 33.33 | | 2 | Jane Doe | 90.90 | +-----------+----------+--------------------+ </code></pre> <p>Since you need the portfolio ID, things get more complicated. Let's do it in steps. First, we'll write a subquery that returns the maximal portfolio value for each client:</p> <pre><code>select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id +-----------+----------+ | client_id | maxtotal | +-----------+----------+ | 1 | 33.33 | | 2 | 90.90 | +-----------+----------+ </code></pre> <p>Then we'll query the portfolio table, but use a join to the previous subquery in order to keep only those portfolios the total value of which is the maximal for the client:</p> <pre><code> select portfolio_id, cash + stocks from portfolio join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal +--------------+---------------+ | portfolio_id | cash + stocks | +--------------+---------------+ | 5 | 33.33 | | 6 | 33.33 | | 8 | 90.90 | +--------------+---------------+ </code></pre> <p>Finally, we can join to the client table (as you did) in order to include the name of each client:</p> <pre><code>select client_id, name, portfolio_id, cash + stocks from client join portfolio using (client_id) join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal +-----------+----------+--------------+---------------+ | client_id | name | portfolio_id | cash + stocks | +-----------+----------+--------------+---------------+ | 1 | John Doe | 5 | 33.33 | | 1 | John Doe | 6 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | +-----------+----------+--------------+---------------+ </code></pre> <p>Note that this returns two rows for John Doe because he has two portfolios with the exact same total value. To avoid this and pick an arbitrary top portfolio, tag on a GROUP BY clause:</p> <pre><code>select client_id, name, portfolio_id, cash + stocks from client join portfolio using (client_id) join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal group by client_id, cash + stocks +-----------+----------+--------------+---------------+ | client_id | name | portfolio_id | cash + stocks | +-----------+----------+--------------+---------------+ | 1 | John Doe | 5 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | +-----------+----------+--------------+---------------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload