Mysql SQL-将结果列中的所有值相加,并使用同一查询中的另一列计算百分比

Mysql SQL-将结果列中的所有值相加,并使用同一查询中的另一列计算百分比,mysql,sql,Mysql,Sql,我发现很难在标题中描述我的问题,但在这里我会更清楚。我有两个表users\u friends和active\u users。。。这是两者的模式 CREATE TABLE `users_friends` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, `friend_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQ

我发现很难在标题中描述我的问题,但在这里我会更清楚。我有两个表
users\u friends
active\u users
。。。这是两者的模式

CREATE TABLE `users_friends` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `friend_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `friend_id_user_id_index` (`friend_id`,`user_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7967354 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


CREATE TABLE `active_users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `first` varchar(155) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=150948970 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

我想查看跟踪1人、2人、3人等的
活跃用户的百分比

我运行了这个查询,它得到了X个用户之后的用户数

( SELECT count(t.total_follows) AS users_count, t.total_follows FROM ( ( SELECT count(*) total_follows, active_users.id FROM active_users INNER JOIN users_friends on users_friends.user_id = active_users.id GROUP BY active_users.id ) AS t ) GROUP BY t.total_follows ORDER BY t.total_follows )
以下是结果

users_count   total_follows
   5                   1
   3                   2
   2                   3
我可以通过修改上面的查询并对
users\u count
进行如下汇总,来获得
users\u count
列的总和

select SUM(t1.users_count) as total_sum from ( < insert above query> ) as t1
我知道这样做可以得到这个列的值

(count(t.total_follows) / 10) * 100 as percentage

但是我不能在我的查询中硬编码总数(10)。我需要查询在1次运行中计算所有内容。如何修改查询以实现此目的?

您可能希望尝试使用“with”语句:

WITH x as
( 
SELECT count(t.total_follows) AS users_count, 
t.total_follows 
FROM ( ( SELECT count(*) total_follows, active_users.id 
FROM active_users 
INNER JOIN users_friends on users_friends.user_id = active_users.id 
GROUP BY active_users.id ) AS t ) 
GROUP BY t.total_follows 
ORDER BY t.total_follows )
)

SELECT *, COUNT/(SELECT SUM(users_count) FROM x)::FLOAT percentage from x

最后我只计算了所有列的总和,并将它们存储在一个名为
@total\u sum
的变量中,然后在第二个查询中使用它。。。如果我能像@Stoddard Meigs那样使用
语句就好了,但是我的mysql版本不支持它

set @total_sum = (select SUM(t1.users_count) as total_sum from ( ( SELECT 
count(table1.total_follows) AS users_count, table1.total_follows FROM ( ( 
SELECT count(*) total_follows, active_users.id FROM active_users INNER JOIN 
users_friends on users_friends.user_id = active_users.id GROUP BY 
active_users.id ) AS table1 ) GROUP BY table1.total_follows ORDER BY 
table1.total_follows ) ) as t1);

( SELECT count(table1.total_follows) AS users_count, table1.total_follows, 
(count(table1.total_follows) / @total_sum) * 100 as percentage FROM ( ( SELECT 
count(*) total_follows, active_users.id FROM active_users INNER JOIN 
users_friends on users_friends.user_id = active_users.id GROUP BY 
active_users.id ) AS table1 ) GROUP BY table1.total_follows ORDER BY 
table1.total_follows )

如果我的mysql版本支持WITH子句:/,这可能会非常有效。。。感谢您为响应提供的responseThanks,但这在语法上是不正确的,因为它将是另一个select子句@urderboy中的select子句
set @total_sum = (select SUM(t1.users_count) as total_sum from ( ( SELECT 
count(table1.total_follows) AS users_count, table1.total_follows FROM ( ( 
SELECT count(*) total_follows, active_users.id FROM active_users INNER JOIN 
users_friends on users_friends.user_id = active_users.id GROUP BY 
active_users.id ) AS table1 ) GROUP BY table1.total_follows ORDER BY 
table1.total_follows ) ) as t1);

( SELECT count(table1.total_follows) AS users_count, table1.total_follows, 
(count(table1.total_follows) / @total_sum) * 100 as percentage FROM ( ( SELECT 
count(*) total_follows, active_users.id FROM active_users INNER JOIN 
users_friends on users_friends.user_id = active_users.id GROUP BY 
active_users.id ) AS table1 ) GROUP BY table1.total_follows ORDER BY 
table1.total_follows )