简体   繁体   English

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

[英]SQL- Add up all the values in a resulting column and calculate the percentage using another column in the same query

I found it quite difficult to describe my problem in the title, but I will be more clear here. 我发现很难在标题中描述我的问题,但是在这里我会更加清楚。 I have two tables users_friends and active_users ... here is the schema for both 我有两个表users_friendsactive_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;

I want to see the percentage of active_users who are following 1 person, 2 people, 3 people, etc, etc 我想查看关注1个人,2个人,3个人等的active_users的百分比

I ran this query which gets me the number of users following X number of people 我运行了此查询,使我得到了跟随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 )

Here are the results 这是结果

users_count   total_follows
   5                   1
   3                   2
   2                   3

I can get the total sum of the users_count column by modifying the query above and summing up the users_count like this 我可以通过修改上面的查询并像这样汇总users_count来获得users_count列的总和

select SUM(t1.users_count) as total_sum from ( < insert above query> ) as t1

but I don't know how to calculate the SUM of the users_count column divided by the users_count which would get me the desired result shown below 但我不知道如何计算users_count列的总和除以users_count ,这将为我提供如下所示的所需结果

users_count    total_follows   percentage
   5                  1           50.0
   3                  2           30.0
   2                  3           20.0        

I know I can get this column value by doing this 我知道我可以通过这样做来获得此列值

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

but I can't hardcode the total sum ( 10 ) in my query. 但我无法对查询中的总和(10)进行硬编码。 I need the query to calculate everything in 1 run. 我需要查询才能在1次运行中计算所有内容。 How can I modify my query to achieve this ? 如何修改查询以实现此目的?

You might want to try using a "with" statement: 您可能想尝试使用“ 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

I ended up just computing the total sum of all the columns and storing them in a variable called @total_sum and then using that in a second query ... would have been nice if I could use the WITH statement like @Stoddard Meigs proposed, but my version of mysql doesn't support it 我最终只是计算所有列的总和,并将它们存储在一个名为@total_sum的变量中,然后在第二个查询中使用它……如果我可以使用WITH语句(如@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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM