简体   繁体   中英

Calculating (dividing) two summed fields in SQL Query

I have this query:

Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage 
 from [AI_DataMart].[AzureViews].[v_FactMeteredUsageMonthly] as FMUM
join [AI_DataMart].[AzureViews].[v_DimServiceExtended] as SE on(FMUM.DimServiceKey = SE.DimServiceKey)
join [AI_DataMart].[AzureViews].[v_DimAccount] as A on(FMUM.DimAccountKey = A.DimAccountKey)
Join [AI_DataMart].[AzureViews].[v_DimSubscription] as SU on(FMUM.DimSubscriptionKey = SU.DimSubscriptionKey)
where 
 FMUM.DimDateKey >= '20150201'And FMUM.DimDateKey <= '20150331'
And SE.Workload = 'SQLDB'
And SE.ResourceName != 'SQL Reporting Hours'
And (SU.AI_BillingType = 'EA' or SU.AI_BillingType = 'Direct') 
 AND SU.IsFraudIdentified = 0
AND SU.AI_IsTest = 0
AND SU.DimBillingSystemKey = 1
AND FMUM.DimSubscriptionKey = '4707785'
group by FMUM.DimSubscriptionKey

Which Returns this result:

DimSubscriptionKey TotalUnits AI_NormalizedUsage 4707785 24.77043700 21.08775630

What I need to get and can't get to work is a 4th Column Which would be (AI_NormaliedUsage / TotalUnits)

But when I add it to the query:

Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage, (AI_NormalizedUsage / TotalUnits) 
 from [AI_DataMart].[AzureViews].[v_FactMeteredUsageMonthly] as FMUM
join [AI_DataMart].[AzureViews].[v_DimServiceExtended] as SE on(FMUM.DimServiceKey = SE.DimServiceKey)
join [AI_DataMart].[AzureViews].[v_DimAccount] as A on(FMUM.DimAccountKey = A.DimAccountKey)
Join [AI_DataMart].[AzureViews].[v_DimSubscription] as SU on(FMUM.DimSubscriptionKey = SU.DimSubscriptionKey)
where 
 FMUM.DimDateKey >= '20150201'And FMUM.DimDateKey <= '20150331'
And SE.Workload = 'SQLDB'
And SE.ResourceName != 'SQL Reporting Hours'
And (SU.AI_BillingType = 'EA' or SU.AI_BillingType = 'Direct') 
 AND SU.IsFraudIdentified = 0
AND SU.AI_IsTest = 0
AND SU.DimBillingSystemKey = 1
AND FMUM.DimSubscriptionKey = '4707785'
group by FMUM.DimSubscriptionKey, (AI_NormalizedUsage / TotalUnits) 

I am not getting expected results:

DimSubscriptionKey  TotalUnits  AI_NormalizedUsage  (No column name)
4707785 1.45831000  0.70567620  0.48389999382847268413
4707785 3.39577900  3.28609533  0.96769999755578911348
4707785 9.49984800  9.19300290  0.96769999898945751553
4707785 3.41661200  3.30625543  0.96769999929754973640
4707785 4.49992800  2.17751515  0.48389999795552284392
4707785 2.49996000  2.41921129  0.96769999919998719979

What I want is

DimSubscriptionKey TotalUnits AI_NormalizedUsge NewCalculatedField<br>
4707785 24.770437   21.0877563  0.851327585

What am I missing? Racking my brain!

if you're referencing derived columns from within the same select statement you can't refer to them by their alias, you need to either duplicate the calculation, or wrap it in another select statement.

Without knowing anything about the underlying tables, this fix should work:

Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage, sum(FMUM.AI_NormalizedUsage) / sum(FMUM.TotalUnits) NewCalculatedField 
 from [AI_DataMart].[AzureViews].[v_FactMeteredUsageMonthly] as FMUM
join [AI_DataMart].[AzureViews].[v_DimServiceExtended] as SE on(FMUM.DimServiceKey = SE.DimServiceKey)
join [AI_DataMart].[AzureViews].[v_DimAccount] as A on(FMUM.DimAccountKey = A.DimAccountKey)
Join [AI_DataMart].[AzureViews].[v_DimSubscription] as SU on(FMUM.DimSubscriptionKey = SU.DimSubscriptionKey)
where 
 FMUM.DimDateKey >= '20150201'And FMUM.DimDateKey <= '20150331'
And SE.Workload = 'SQLDB'
And SE.ResourceName != 'SQL Reporting Hours'
And (SU.AI_BillingType = 'EA' or SU.AI_BillingType = 'Direct') 
 AND SU.IsFraudIdentified = 0
AND SU.AI_IsTest = 0
AND SU.DimBillingSystemKey = 1
AND FMUM.DimSubscriptionKey = '4707785'
group by FMUM.DimSubscriptionKey

Sorry I can't comment but have you tried this:

Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage, (sum(FMUM.AI_NormalizedUsage / sum(FMUM.TotalUnits))

Should not need the group by either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM