。
假设我有以下表格:
Account | Revenue | Sales Region
--------|---------|--------------
Toyota | 718935 | US - East
--------|---------|--------------
Safeway | 327895 | US - West
--------|---------|--------------
MLB | 1028943 | US - East
&
Sales Region | Total Revenue
-------------|---------------
US - East | 1747878
-------------|---------------
US - West | 327895
当收入进入 MLB 帐户时,我将如何在两个销售区域之间分配收入?因此,"美国 - 东部"将获得全部 1028943 美元,而不是两个销售区域都将获得 514471.50 美元
解决它的一种方法,因为您唯一的标准是硬编码拆分的名称是:
select a.region,
sum(a.revenue) + ( (select sum(revenue)
from accounts
where name in ('MLB')
)/
(select count(*)
from accounts
where name not in ('MLB')
)
)
from accounts a
where a.name not in ('MLB')
group by a.region
请注意,我对这些子查询使用了IN/NOT IN
,以防您决定拆分多个帐户。
在这里看到它的工作:http://sqlfiddle.com/#!17/7ef3a/13