MySQL由两列组成的组
我试图在这里按多个列进行分组—每个表上有一列。MySQL由两列组成的组,sql,mysql,database,Sql,Mysql,Database,我试图在这里按多个列进行分组—每个表上有一列。 在这种情况下,我希望通过将客户的当前投资组合和现金相加,找到每个客户的最高投资组合价值,但客户可能有多个投资组合,因此我需要每个客户的最高投资组合 目前,使用下面的代码,我将为每个顶级投资组合多次获得相同的客户(不是按客户id分组) 首先,让我们做一些测试数据: create table client (client_id integer not null primary key auto_increment,
在这种情况下,我希望通过将客户的当前投资组合和现金相加,找到每个客户的最高投资组合价值,但客户可能有多个投资组合,因此我需要每个客户的最高投资组合 目前,使用下面的代码,我将为每个顶级投资组合多次获得相同的客户(不是按客户id分组)
首先,让我们做一些测试数据:
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 |
+-----------+----------+
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 |
+-----------+----------+--------------+---------------+
然后我们将查询portfolio表,但使用前一个子查询的联接,以便只保留那些对客户端来说总价值最大的投资组合:
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, max(cash + stocks) as maxtotal
from portfolio
group by client_id
+-----------+----------+
| client_id | maxtotal |
+-----------+----------+
| 1 | 33.33 |
| 2 | 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返回两行,因为他有两个总价值完全相同的投资组合。要避免这种情况并选择任意的顶级投资组合,请标记GROUPBY子句:
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 |
+-----------+----------+--------------+---------------+
在group by上使用Concat将起作用
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 CONCAT(portfolios.id, "-", clients.id)
ORDER BY total DESC
LIMIT 30
这是一个令人惊讶的答案,一旦我把maxtotal DESC的订单放在最后,它就完美了!;)我想说,谢谢你,聪明。对我来说也是很好的解决方案!我会在那里添加一个delimeter:CONCAT(portfolions.id,“-”,clients.id)。如果没有delimeter,可能会有相同的连接值和不同的值对。正如maksa所说,如果您碰巧遇到投资组合1属于客户机11(concat:'111')而投资组合11属于客户机1(concat:'111')的情况,则分隔符非常重要然后它们将被分组在一起。虽然
CONCAT
确实有很高的性能成本,但正如另一个答案所建议的那样,它可能比使用子查询性能更好……还有CONCAT_WS('-',field,field)
到具有指定分隔符的CONCAT字段。