Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql按所有总和分组_Sql_Oracle_Group By - Fatal编程技术网

sql按所有总和分组

sql按所有总和分组,sql,oracle,group-by,Sql,Oracle,Group By,我有一个查询(针对Oracle格式化): 汇总五月份的发票。结果类似于: $446,088.62 Bob $443,439.29 Sally $275,097.00 Tom $95,170.00 George $53,150.00 Jill 但是,我需要将每一笔金额除以这些金额的总和($1312944.91),结果是: $446,088.62 34% Bob $443,439.29 34% Sally $275,097.00

我有一个查询(针对Oracle格式化):

汇总五月份的发票。结果类似于:

$446,088.62     Bob
$443,439.29     Sally
$275,097.00     Tom
 $95,170.00     George
 $53,150.00     Jill
但是,我需要将每一笔金额除以这些金额的总和($1312944.91),结果是:

$446,088.62     34%  Bob
$443,439.29     34%  Sally
$275,097.00     21%  Tom
 $95,170.00      7%  George
 $53,150.00      4%  Jill
(百分比列的总和应为100%)


有没有办法在查询中实现这一点?

只需使用分析函数:

select spn.salesperson_name, sum(inv.quantity * inv.price), 
       sum(inv.quantity * inv.price)  / sum(sum(inv.quantity * inv.price)) over () as ratio 
from invoice inv inner join
     salesperson spn
     on spn.spn_id = inv.spn_id
where inc.invoice_date between date '2017-05-01' and date '2017-05-31'
group by spn.salesperson_name;

只需使用分析函数:

select spn.salesperson_name, sum(inv.quantity * inv.price), 
       sum(inv.quantity * inv.price)  / sum(sum(inv.quantity * inv.price)) over () as ratio 
from invoice inv inner join
     salesperson spn
     on spn.spn_id = inv.spn_id
where inc.invoice_date between date '2017-05-01' and date '2017-05-31'
group by spn.salesperson_name;

当存在的函数正好满足您的需要时,最好使用这些函数。在本例中,SQL标准分析函数
RATIO\u TO\u REPORT
(至少在Oracle和SQL Server中实现)正好满足您的需要

具体而言,
select
子句可以是:

select sum(inv.quantity * inv.price) AS TOTAL_REVENUE   --  use column aliases!
     , ratio_to_report(sum(inv.quantity * inv.price)) over () AS RATIO,
     , spn.salesperson_name
from   .......   (rest of your query goes here)
请注意,与公认的答案一样,此解决方案将以小数形式显示比率,而不是百分比(且不四舍五入)。如果您需要附加一个百分号,您将需要转换为字符串。。。如果是这样,下面的技巧(这是一个技巧!)将为您提供您所需要的:

to_char( ratio_to_report(.....), 'fm99L', 'nls_currency = %' ) AS RATIO, .....

to_char
中的
L
元素表示货币符号;您可以将货币符号定义为百分号。

当存在的函数正好满足您的需要时,最好使用这些函数。在本例中,SQL标准分析函数
RATIO\u TO\u REPORT
(至少在Oracle和SQL Server中实现)正好满足您的需要

具体而言,
select
子句可以是:

select sum(inv.quantity * inv.price) AS TOTAL_REVENUE   --  use column aliases!
     , ratio_to_report(sum(inv.quantity * inv.price)) over () AS RATIO,
     , spn.salesperson_name
from   .......   (rest of your query goes here)
请注意,与公认的答案一样,此解决方案将以小数形式显示比率,而不是百分比(且不四舍五入)。如果您需要附加一个百分号,您将需要转换为字符串。。。如果是这样,下面的技巧(这是一个技巧!)将为您提供您所需要的:

to_char( ratio_to_report(.....), 'fm99L', 'nls_currency = %' ) AS RATIO, .....

to_char
中的
L
元素表示货币符号;您将货币符号定义为百分率符号。

您可能还想考虑<代码> RealoToToRePoT()/>代码>我使用它发布了一个解决方案。您可能还想考虑<代码> RealoSoToRePoT()/代码>——我用它发布了一个解决方案。我同意。这是一个更好的答案,因为函数是精确的,使代码更简洁。我同意。这是一个更好的答案,因为函数是精确的,并且使代码更简洁。