GROUP BY SQL Server 后总和的百分比

Percentage from Total SUM after GROUP BY SQL Server(GROUP BY SQL Server 后总和的百分比)
本文介绍了GROUP BY SQL Server 后总和的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些结果:

PersonID    SUM(PA.Total)
-------------------------
   1            75
   2            75
   3            15
   4            15
   5            60
   6            60

像这样的表:

PersonID    Total
------------------
   1         50
   2         50
   3         10
   4         10
   5         40
   6         40
   1         25
   2         25
   3          5
   4          5
   5         20
   6         20

这些是按人分组的.现在,我希望添加一列,其中包含根据所有总和计算得出的每个人的百分比.

These are grouped by the person. Now I'm looking to add a column with the percentages for each person calculated from the total of all of their sums.

例如:总和是 300,因此我需要这样的结果:

For example: the total sum is 300, and hence I need a result like this:

PersonID    SUM(PA.Total)   Percentage
--------------------------------------
   1            75              25%
   2            75              25%
   3            15              5%
   4            15              5%
   5            60              20%
   6            60              20%

我在网上查看了代码,并想出了一个修复方法,例如:

I have looked at code online and I have come up with a fix such as this:

 SELECT 
     P.PersonID, SUM(PA.Total)
     SUM(PA.Total) * 100 / [p] AS 'Percentage'
 FROM 
     Person P
 JOIN 
     Package PA ON P.PersonID = PA.PackageFK
 CROSS JOIN 
     (SELECT SUM(PA.[Total]) AS [p] 
      FROM Package PA) t
 GROUP BY 
     P.PersonID

但我不确定如何将交叉联接合并到联接以及已经分组/求和部分中.或者这是否完全正确.

But I'm unsure how to incorporate the cross join into the join as well as the already group/sum section. Or whether this is along the right lines altogether.

任何帮助将不胜感激 - SQL fiddle http://sqlfiddle.com/#!9/80f91/2

Any help would be appreciated - SQL fiddle http://sqlfiddle.com/#!9/80f91/2

推荐答案

你不需要 cross join.只需使用窗口函数:

You don't need a cross join. Just use window functions:

SELECT P.PersonID, SUM(PA.Total),
       SUM(PA.Total) * 100.0 / SUM(SUM(PA.Total)) OVER () AS Percentage
FROM Person P JOIN
     Package PA
     ON P.PersonID = PA.PackageFK
GROUP BY P.PersonID;

请注意,此查询不需要 JOIN:

Note that you do not need the JOIN for this query:

SELECT PA.PersonID, SUM(PA.Total),
       SUM(PA.Total) * 100.0 / SUM(SUM(PA.Total)) OVER () AS Percentage
FROM Package PA
GROUP BY PA.PersonID;

SQL Server 进行整数除法.我使用十进制数进行此类计算,因此它们更有意义.

SQL Server does integer division. I do such calculations using decimal numbers so they make more sense.

这里是一个 SQL Fiddle,有两个变化:

Here is a SQL Fiddle, with two changes:

  1. 数据库更改为 SQL Server.
  2. 总数存储为数字而不是字符串.

这篇关于GROUP BY SQL Server 后总和的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)