简体   繁体   English

使用SQL计算百分比

[英]Calculating percentages with SQL

I'm trying to create an SQL query to work out the percentage of rows given its number of play counts. 我正在尝试创建一个SQL查询,以根据给定的播放次数计算出行的百分比。

My DB currently has 800 rows of content, All content has been played a total of 3,000,000 times put together 我的数据库目前有800行内容,所有内容总共播放了3,000,000次

table: id, play_count, content 表格: id,play_count,内容

Lets say I'd like to work out the percentage of the first 10 rows. 可以说我想算出前10行的百分比。

My attempts have looked similar to this: 我的尝试看起来与此类似:

 SELECT COUNT(*) AS total_content,
 SUM(play_count) AS total_played,
 content.play_count AS content_plays
 FROM bebo_video

How would I put this all together to show a final percentage on each individual row?? 我如何将所有这些放在一起以在每一行上显示最终百分比?

SELECT play_count / (SELECT SUM(play_count) FROM bebo_video) * 100 FROM bebo_video

使用ROUND,TRUNCATE等格式化结果值。

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

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