在这个数据仓库中,我们有一个由多个组织组成的组织,
我有[FactFinance]表,其中包含有关每个组织收入的信息。
我在数据仓库中有以下查询,它从[组织维度表]中选择(组织名称),从[FactFinance表]中选择其(总收入金额),以及它(总收入金额)表示的百分比来自整个组织的总收入金额
select [DimOrganization].[OrganizationName] , SUM([Amount])as TotalAmountIncome , SUM([FactFinance].[Amount])/(select sum([FactFinance].[Amount])
from [FactFinance] ) PercentageOfTotalSales
from [FactFinance] , [DimOrganization]
Where [FactFinance].[OrganizationKey]= [DimOrganization].[OrganizationKey]
group by ([DimOrganization].[OrganizationName])
order by SUM([Amount]) desc
结果如下表所示:
+--------------------+-------------------+------------------------+
| OrganizationName | TotalAmountIncome | PercentageOfTotalSales |
+--------------------+-------------------+------------------------+
| Canadian Division | 292174782.72 | 0.215049383184007 |
| Southeast Division | 279284663.6 | 0.205561869784944 |
| Southwest Division | 232365970.6 | 0.171028307731715 |
| Central Division | 149032081.6 | 0.109692071726198 |
| Northwest Division | 138922888.8 | 0.102251403315702 |
| Northeast Division | 134003346.4 | 0.0986304728958399 |
| France | 66963799.96 | 0.0492873606099529 |
| Australia | 35553074.99 | 0.0261681270906303 |
| Germany | 30339804.03 | 0.0223310036610101 |
+--------------------+-------------------+------------------------+
我想修改此查询,以便只选择收入最高且占整个组织总收入近60%的组织。
如果我计算前三行中的百分比,即具有最大收入的组织
+------------------------+
| PercentageOfTotalSales |
+------------------------+
| 0.215049383 |
| 0.20556187 |
| 0.171028308 |
+------------------------+
结果将是:0.591639561 几乎是60%
所以结果必须是前三行(这意味着整个组织总收入的近60%?)
我尝试了以下查询,但它给了我错误(无效的列名'PercentageOfTotalSales')。
select [DimOrganization].[OrganizationName] , SUM([Amount])as TotalAmountIncome , SUM([FactFinance].[Amount])/(select sum([FactFinance].[Amount])
from [FactFinance] ) PercentageOfTotalSales
from [FactFinance] , [DimOrganization]
Where [FactFinance].[OrganizationKey]= [DimOrganization].[OrganizationKey]
AND SUM(PercentageOfTotalSales) >= 0.60
group by ([DimOrganization].[OrganizationName])
order by SUM([Amount]) desc
我尝试了不同的方法,但总是给我错误。
我可以得到任何建议吗?
答案 0 :(得分:1)
您可以使用Running Total查询并将返回的表用作子查询,如下所示:
SELECT [DimOrganization].OrganizationName , Value from (
SELECT x, Value, (SELECT SUM(Value) FROM (select top 10 [FactFinance].[OrganizationKey] as x,SUM([Amount]) as [Amount] , SUM([Amount]) /
(select sum([Amount]) from [FactFinance])as Value
FROM [FactFinance] group by([FactFinance].[OrganizationKey]) order by SUM([Amount]) desc) T2
WHERE T1.value <= T2.value) AS RunningTotal
FROM (select top 10 [FactFinance].[OrganizationKey] as x,SUM([FactFinance].[Amount])as [Amount] , SUM([FactFinance].[Amount])/(select sum([FactFinance].[Amount])
from [FactFinance])as Value
from [FactFinance] group by([FactFinance].[OrganizationKey]) order by SUM([FactFinance].[Amount]) desc) T1 ) allTable , [DimOrganization] where allTable.x = [DimOrganization].OrganizationKey AND RunningTotal<0.6
答案 1 :(得分:0)
如果没有要测试的表结构,这应该可行。
SELECT [DimOrganization].[OrganizationName]
,SUM([Amount])as TotalAmountIncome
,SUM([FactFinance].[Amount])/TotalSales.Amount) PercentageOfTotalSales
FROM [FactFinance]
INNER JOIN [DimOrganization]
ON [FactFinance].[OrganizationKey]= [DimOrganization].[OrganizationKey]
OUTER APPLY (
SELECT SUM([FactFinance].[Amount]) Amount
FROM [FactFinance]
) TotalSales
GROUP BY [DimOrganization].[OrganizationName]
HAVING SUM(PercentageOfTotalSales) >= 0.60
ORDER BY SUM([Amount]) DESC
对于不同的百分比,您可以使用视图并传入一系列可容忍的值来实现它。