sql - MySQL GROUP BY two columns -


I'm trying to group with multiple columns - one on each table.
This is a scenario where I want to combine our existing portfolio and cash for each customer to get the top portfolio value together, but there can be more than one portfolio in a client, so I have a headline for each customer. Portfolio

At this time, I am receiving the same client multiple times for each of my top portfolio with the code (this is not grouped by client ID).

  select clients.id, clients.name, portfolios.id, SUM (Portfolio Portfolio + Portfolio. CAS), Portfolio, Where Client.ID = Portfolio.Clit_ID Group By Portfolio ID, clients.IDER Total 30 by DESC serials Create a Client (ClientIID integer zero primary key is not auto_increment, name varchar (64) )); Create table portfolio (portfolio_id integer zero primary key auto_increment, client_id integer refers to client.id, cash decimal (10,2), share decimals (10,2)); Enter the client (name) value ('John Doe'), ('Jane Doe'); Enter the portfolio (client_id, cash, stock) price (1, 11.11, 22.22), (1, 10.11, 23.22), (2, 30.30, 40.40), (2, 40.40, 50.50);  

If you did not need a portfolio ID, then it would be easy:

  Customer ID, name, maximum (cash + share) Select Portfolio by customer customer (customer_id) ----------- + ---------- + ---------------- - - + | Client_id | Name | Maximum (Cash + Share) | + ----------- + ---------- + -------------------- + | 1 | John Doe | 33.33 | | 2 | Jane Doe | 90.90 | + ----------- + ---------- + -------------------- +  < / Pre> 

Since you need a portfolio ID, things will be more complicated. Let's do it in the steps First of all, we will write a subquery which gives maximum value to each customer: Select client_id, maximum (cash + shares) as max from client group by client_id. ----- ------ + ---------- + | Client_id | Maximum + ----------- + ---------- + | 1 | 33.33 | | 2 | 90.90 | + ----------- + ---------- +

Then we will ask the question of the portfolio table, but use it to join To retain only those portfolios, the maximum value for the customer in the previous subclass is maximum:

  Select the portfolio to join the portfolio by joining the portfolio (select client_id, max (Cash + stock) as max max by using client_id from portfolio group (client) _id) where cash + share = maximum - + -------------- + -------------- - + | Portfolio_id | Cash + Share | + -------------- + --------------- + | 5 | 33.33 | | 6 | 33.33 | | 8 | 90.90 | + -------------- + --------------- +  

Finally, we are in Customer Client (as you did) to include the name of client client:

  Connect client-connected client ID, name, portfolio_id, cache + use stock (client_id) Join the portfolio using (select customer ID, max (cash + share) as maximizing portfolio portfolio as customer) using maxima (client_id) where Cash + Share = Max - + ----------- + ---------- + -------------- + ----- ---------- + | Client_id | Name | Portfolio_id | Cash + Share | + ----------- + ---------- + -------------- + ----------- ---- + | 1 | John Doe | 5 | 33.33 | | 1 | John Doe | 6 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | + ----------- + ---------- + -------------- + ----------- ---- +  

Note that this gives two lines for John Do because they have two portfolios, who have the exact total value. To avoid this and select an arbitrary top portfolio, tag on the group by clause:

connect client (id), name, portfolio_id, cache + max (cache + shares) ) As Maxtotal by client_id from portfolio group as maximum usage (client_id) where cash + share = maxtotal group by client_id, cash + stock + ----------- + ----- ----- + -------------- + --------------- + | Client_id | Name | Portfolio_id | Cash + Share | + ----------- + ---------- + -------------- + ----------- ---- + | 1 | John Doe | 5 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | + ----------- + ---------- + -------------- + ----------- ---- +

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -