sql - MySQL GROUP BY 两列

我在这里尝试按多列进行分组 - 每张 table 上一个。
在这种情况下,我想通过将每个客户当前的投资组合和现金相加来找到每个客户的最高投资组合值(value),但客户可能拥有多个投资组合,因此我需要每个客户的顶级投资组合。

目前,通过下面的代码,我为他们的每个顶级投资组合多次获得相同的客户(它不是按客户 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 

最佳答案

首先,让我们做一些测试数据:

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

如果您不需要投资组合 ID,这很容易:

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

由于您需要投资组合 ID,因此事情变得更加复杂。让我们分步进行。首先,我们将编写一个子查询,返回每个客户的最大投资组合值:

select client_id, max(cash + stocks) as maxtotal
from portfolio
group by client_id

+-----------+----------+
| client_id | maxtotal |
+-----------+----------+
|         1 |    33.33 | 
|         2 |    90.90 | 
+-----------+----------+

然后我们将查询投资组合表,但使用与前一个子查询的连接,以便仅保留总值(value)为客户最大值的投资组合:

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

最后,我们可以加入客户表(如您所做的那样)以包含每个客户的名称:

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

请注意,这会为 John Doe 返回两行,因为他有两个总值(value)完全相同的投资组合。为了避免这种情况并选择任意的顶级投资组合,请在 GROUP BY 子句上添加标签:

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

https://stackoverflow.com/questions/2183373/

相关文章:

mysql - InnoDB 需要一个多小时来导入 600MB 文件,MyISAM 在几分钟内

mysql - 我的 SQL 有什么问题? #1089 - 不正确的前缀键

mysql - 如何在不丢失列数据的情况下更改 MySQL 表的列位置?

python - 如何将数据插入 MySQL 数据库?

mysql - SQL 按计数排序

sql - 如何查找具有包含小写字母的值的行

mysql - 无法通过 phpmyadmin 导入数据库 文件过大

sql - 从表中删除所有

mysql - MySQL中给定子字符串的最后一个索引

sql - MySQL:比较两个表之间的差异