Select 收入占总收入约 60% 的组织 SQL Server 2008
Select organizations that their income represent around 60% of the total income SQL Server2008
在这个数据仓库中,我们有由多个组织组成的组织,
我有 [FactFinance] table,其中包含每个组织的收入信息。
我在数据仓库中有以下查询,其中 select 来自 [组织维度 table] 的(组织名称)及其来自 [FactFinance table 的(总收入) ]及其(总收入金额)占整个组织总收入金额的百分比
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
结果如下table:
+--------------------+-------------------+------------------------+
| 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 |
+--------------------+-------------------+------------------------+
我想修改此查询,以便 select 仅显示收入最高且几乎占整个组织总收入 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
我尝试了不同的方法,但它总是给我错误。
我能得到什么建议吗?
没有 table 结构来测试,这应该可行。
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
至于变化的百分比,您可以使用视图并传入一个可接受的值范围来实现它。
您可以使用 运行 总查询并将返回的 table 用作子查询,如下所示:
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
在这个数据仓库中,我们有由多个组织组成的组织,
我有 [FactFinance] table,其中包含每个组织的收入信息。
我在数据仓库中有以下查询,其中 select 来自 [组织维度 table] 的(组织名称)及其来自 [FactFinance table 的(总收入) ]及其(总收入金额)占整个组织总收入金额的百分比
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
结果如下table:
+--------------------+-------------------+------------------------+
| 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 |
+--------------------+-------------------+------------------------+
我想修改此查询,以便 select 仅显示收入最高且几乎占整个组织总收入 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
我尝试了不同的方法,但它总是给我错误。
我能得到什么建议吗?
没有 table 结构来测试,这应该可行。
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
至于变化的百分比,您可以使用视图并传入一个可接受的值范围来实现它。
您可以使用 运行 总查询并将返回的 table 用作子查询,如下所示:
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