gpt4 book ai didi

mysql - 如何对 GROUP By 的结果求和

转载 作者:行者123 更新时间:2023-11-29 06:27:37 25 4
gpt4 key购买 nike

我正在尝试添加 MySQL GROUP BY 的结果,但我无法做到这一点,这是我的代码:

SELECT country,SUM(visits) AS visits,SUM(visits-1) AS repetidos, COUNT(1) AS total FROM stats GROUP BY country HAVING COUNT(1) > 1

附上我的代码结果的图片以及我希望通过以下方式实现的目标:

enter image description here

我还会捕获我的表格:

enter image description here

最佳答案

也许您需要添加汇总

drop table if exists t;
create table t
(country varchar(2),visits int);
insert into t values
('co',12),('co',20),
('us',15),('us',1),('us',1);

SELECT country,SUM(visits) AS visits,
SUM(visits-1) AS repetidos,
COUNT(1) AS total
FROM t
GROUP BY country with rollup
HAVING COUNT(1) > 1;

+---------+--------+-----------+-------+
| country | visits | repetidos | total |
+---------+--------+-----------+-------+
| co | 32 | 30 | 2 |
| us | 17 | 14 | 3 |
| NULL | 49 | 44 | 5 |
+---------+--------+-----------+-------+
3 rows in set (0.00 sec)

关于mysql - 如何对 GROUP By 的结果求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58577642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com