Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 579549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:22:42+00:00 2026-05-13T14:22:42+00:00

I’m trying to group by multiple columns here – one on each table. It’s

  • 0

I’m trying to group by multiple columns here – one on each table.
It’s a scenario where I want to find the top portfolio value for each client by adding their current portfolio and cash together but a client may have more than one portfolio, so I need the top portfolio for each client.

At the moment, with the code below I’m getting the same clients multiple times for each of their top portfolios (it’s not grouping by client id).

SELECT clients.id, clients.name, portfolios.id, SUM ( portfolios.portfolio +  portfolios.cash ) AS total
FROM clients, portfolios
WHERE clients.id = portfolios.client_id
GROUP BY portfolios.id, clients.id
ORDER BY total DESC
LIMIT 30 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:22:43+00:00Added an answer on May 13, 2026 at 2:22 pm

    First, let’s make some test data:

    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);
    

    If you didn’t need the portfolio ID, it would be easy:

    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 | 
    +-----------+----------+--------------------+
    

    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:

    select client_id, max(cash + stocks) as maxtotal
    from portfolio
    group by client_id
    
    +-----------+----------+
    | client_id | maxtotal |
    +-----------+----------+
    |         1 |    33.33 | 
    |         2 |    90.90 | 
    +-----------+----------+
    

    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:

     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 | 
    +--------------+---------------+
    

    Finally, we can join to the client table (as you did) in order to include the name of each client:

    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 | 
    +-----------+----------+--------------+---------------+
    

    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:

    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 | 
    +-----------+----------+--------------+---------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Stop re-inventing the wheel! This has already been done -… May 14, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer Check out this blog post. Basically, it randomizes the RHS… May 14, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer There is no real ascii codes for these keys as… May 14, 2026 at 10:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.