使用SQL查询进行的计算与使用数字进行的手动SQL计算不匹配

时间:2019-04-18 15:22:34

标签: sql-server

我有一个SQL查询,该查询从数据库中提取和/或计算许多值,然后根据这些值计算另一个数。当我运行此查询时,我收到一个值。但是,当我使用单个数字并在单独的查询中使用相同的公式时,这些数字将得到不同的值。

偏差超过5%,因此我发现舍入不太可能造成如此大的误差。

除了运行手动SQL查询之外,我还使用数字本身来使用Excel运行手动计算。 Excel计算与手动数值计算一致。

查询本身如下。

SELECT DISTINCT
h.Annual_Usage_ekWh AS Energy_1,
i.Annual_Usage_ekWh AS Energy_2,
n.Annual_Usage_ekWh AS Energy_3,
o.Annual_Usage_ekWh AS Energy_4,
t.Annual_Usage_ekWh AS Energy_5,
u.Annual_Usage_ekWh AS Energy_6,
z.Annual_Usage_ekWh AS Energy_7,
a1.Annual_Usage_ekWh AS Energy_8,
f1.Annual_Usage_ekWh AS Energy_9,
g1.Annual_Usage_ekWh AS Energy_10,
l1.Annual_Usage_ekWh AS Energy_11,
m1.Annual_Usage_ekWh AS Energy_12,
r1.Enclosed_Parking_Area,
CAST(r1.Enclosed_Parking_Area*1.49*0.28 AS decimal(38,16)) as Parking_Lot_Energy,
(SELECT SUM(HDD_15) FROM View_All_Daily_HDD_CDD WHERE Weather_Reference_City_ID=845 AND YEAR(Reference_Date)=2016) AS Current_HDD,
(SELECT SUM(HDD_15) FROM View_All_Daily_HDD_CDD WHERE City LIKE 'Toronto' AND YEAR(Reference_Date)=2009) AS Base_HDD,
(SELECT SUM(CDD_15) FROM View_All_Daily_HDD_CDD WHERE Weather_Reference_City_ID=845 and YEAR(Reference_Date)=2016) AS Current_CDD,
(select SUM(CDD_15) FROM View_All_Daily_HDD_CDD WHERE City LIKE 'Toronto' AND YEAR(Reference_Date)=2009) AS Base_CDD,
c.GFA,

CASE WHEN h.Annual_Usage_ekWh is null AND i.Annual_Usage_ekWh is null AND n.Annual_Usage_ekWh is null AND o.Annual_Usage_ekWh is null
AND t.Annual_Usage_ekWh is null AND u.Annual_Usage_ekWh is null AND z.Annual_Usage_ekWh is null AND a1.Annual_Usage_ekWh is null
AND f1.Annual_Usage_ekWh is null AND g1.Annual_Usage_ekWh is null AND l1.Annual_Usage_ekWh is null AND m1.Annual_Usage_ekWh is null THEN null
WHEN a.Asset_Class not like 'Office' THEN null ELSE

((CASE WHEN h.Annual_Usage_ekWh is null THEN 0 ELSE h.Annual_Usage_ekWh end)
+(CASE WHEN i.Annual_Usage_ekWh is null THEN 0 ELSE i.Annual_Usage_ekWh end)
+(CASE WHEN n.Annual_Usage_ekWh is null THEN 0 ELSE n.Annual_Usage_ekWh end)
+(CASE WHEN o.Annual_Usage_ekWh is null THEN 0 ELSE o.Annual_Usage_ekWh end)
+(CASE WHEN t.Annual_Usage_ekWh is null THEN 0 ELSE t.Annual_Usage_ekWh end)
+(CASE WHEN u.Annual_Usage_ekWh is null THEN 0 ELSE u.Annual_Usage_ekWh end)
+(CASE WHEN z.Annual_Usage_ekWh is null THEN 0 ELSE z.Annual_Usage_ekWh end)
+(CASE WHEN a1.Annual_Usage_ekWh is null THEN 0 ELSE a1.Annual_Usage_ekWh end)
+(CASE WHEN f1.Annual_Usage_ekWh is null THEN 0 ELSE f1.Annual_Usage_ekWh end)
+(CASE WHEN g1.Annual_Usage_ekWh is null THEN 0 ELSE g1.Annual_Usage_ekWh end)
+(CASE WHEN l1.Annual_Usage_ekWh is null THEN 0 ELSE l1.Annual_Usage_ekWh end)
+(CASE WHEN m1.Annual_Usage_ekWh is null THEN 0 ELSE m1.Annual_Usage_ekWh end)
-(CASE WHEN r1.Enclosed_Parking_Area is not null THEN CAST(r1.Enclosed_Parking_Area*1.49*0.28 as decimal(38,16)) ELSE 0 END) 

*

(20 /

((SELECT SUM(HDD_15) FROM View_All_Daily_HDD_CDD WHERE Weather_Reference_City_ID=a.Weather_Reference_City_ID AND YEAR(Reference_Date)=2016)/
(SELECT SUM(HDD_15) FROM View_All_Daily_HDD_CDD WHERE City LIKE 'Toronto' AND YEAR(Reference_Date)=2009)*7.3) +

((SELECT SUM(CDD_15) FROM View_All_Daily_HDD_CDD WHERE Weather_Reference_City_ID=a.Weather_Reference_City_ID AND YEAR(Reference_Date)=2016)/
(SELECT SUM(CDD_15) FROM View_All_Daily_HDD_CDD WHERE City like 'Toronto' AND YEAR(Reference_Date)=2009)*1.3) +

11.4)

*

(20 /

((SELECT (1-e.Vacancy)) * 10.015 + 9.985))

) /c.GFA END as Normalized_EUI_2016

FROM View_All_Buildings a
LEFT JOIN View_All_Building_Ownership_By_Year b ON a.Building_ID=b.Building_ID
LEFT JOIN (SELECT y.Building_ID,y.GFA
FROM View_All_Buildings_Annual_GFA y
INNER JOIN (SELECT x.Building_ID,MAX(x.Reporting_Year) AS Max_Reporting_Year
FROM View_All_Buildings_Annual_GFA x
WHERE x.Tenant=0
GROUP BY x.Building_ID) z on y.Building_ID=z.Building_ID AND y.Reporting_Year=z.Max_Reporting_Year) c ON a.Building_ID=c.Building_ID
LEFT JOIN (select Building_ID,Vacancy 
FROM View_All_Annual_Building_Vacancy
WHERE Reporting_Year = 2016) e ON a.Building_ID=e.Building_ID
LEFT JOIN (select Building_ID,Annual_Usage,
Annual_Usage AS Annual_Usage_ekWh
FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Electricity'
AND Tenant=0
AND Actual_Days>89) h ON a.Building_ID=h.Building_ID
LEFT JOIN (select Building_ID,Annual_Usage,
Annual_Usage AS Annual_Usage_ekWh 
FROM View_Annual_Usage_By_Building_HOOPP_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Electricity'
AND Tenant=1
AND Actual_Days>89) i ON a.Building_ID=i.Building_ID
LEFT JOIN (select Building_ID,Annual_Usage,
CAST(Annual_Usage*(select Factor_ekWh from View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'm3' AND Utility_Type LIKE 'Natural Gas') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type like 'Natural Gas'
AND Tenant=0
AND Actual_Days>89) n ON a.Building_ID=n.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(select Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'm3' AND Utility_Type LIKE 'Natural Gas') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type like 'Natural Gas'
AND Tenant=1
AND Actual_Days>89) o ON a.Building_ID=o.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'lbs' AND Utility_Type LIKE 'Steam') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type like 'Steam'
AND Tenant=0
AND Actual_Days>89) t ON a.Building_ID=t.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'lbs' AND Utility_Type LIKE 'Steam') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Steam'
AND Tenant=1
AND Actual_Days>89) u ON a.Building_ID=u.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'TH' AND Utility_Type LIKE 'Chilled Water') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Chilled Water'
AND Tenant=0
AND Actual_Days>89) z ON a.Building_ID=z.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'TH' AND Utility_Type LIKE 'Chilled Water') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Chilled Water'
AND Tenant=1
AND Actual_Days>89) a1 ON a.Building_ID=a1.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'L' AND Utility_Type LIKE 'Diesel') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Diesel'
AND Tenant=0
AND Actual_Days>89) f1 ON a.Building_ID=f1.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'L' AND Utility_Type LIKE 'Diesel') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Diesel'
AND Tenant=1
AND Actual_Days>89) g1 ON a.Building_ID=g1.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'L' AND Utility_Type LIKE 'Heating Oil') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Heating Oil'
AND Tenant=0
AND Actual_Days>89) l1 ON a.Building_ID=l1.Building_ID
LEFT JOIN (SELECT Building_ID,Annual_Usage,
CAST(Annual_Usage*(SELECT Factor_ekWh FROM View_All_Conversions_Energy 
WHERE Source_Unit LIKE 'L' AND Utility_Type LIKE 'Heating Oil') AS decimal(38,16)) AS Annual_Usage_ekWh FROM View_Annual_Usage_By_Building_2016
WHERE Calendar_Year=2016
AND Utility_Type LIKE 'Heating Oil'
AND Tenant=1
AND Actual_Days>89) m1 ON a.Building_ID=m1.Building_ID
LEFT JOIN (SELECT a.Building_ID,
CASE WHEN a.Response_String is not null THEN
CAST(a.Response_String AS decimal(38,16)) ELSE 0 END AS Enclosed_Parking_Area
FROM Annual_Building_Form_Responses a
WHERE a.Form_Question_ID=65) r1 ON a.Building_ID=r1.Building_ID

where a.Building_ID=189

在此代码块的顶部,我将在Normalized_EUI_2016的计算中使用的每个查询值作为单独的项包括在内,以使计算中使用的值可见。

使用结果图,我使用了一个单独的查询来进行手动计算。

SELECT
(5184075.9999999999999498+5960350.9124000000000000-90040.1040000000000000)
*(20/(3922.4000000000000000/3039.4000000000000000*7.3+206.6000000000000000/496.5000000000000000*1.3+11.4))
*(20/((1-0.0100000000000000000000000000000000000)*10.015+9.985))
/
439865.000000000000000000

请注意,我使用的数字与查询结果中显示的数字完全一样,一直到小数点。

手动计算和我在Excel中执行的计算都同意结果应约为23.64770927687350000。但是,SQL查询输出的值为22.44265500000000000。

查询结果可在https://app.box.com/s/a65wof3h6d1ba8foek3ywfzxjh2h2z7z处参考。

0 个答案:

没有答案