In the code below, it appears that I have a different result whenever I change the order of the element above the denominator. Since these are just simple addition and subtraction operators why would it make a difference how they are ordered. I was finally able to get it woking properly but I am a bit confused about the operatior order of precedence.

(SELECT DV.ForecastYear, @Metric1, ( Sum(case when MetricName=@Metric1 then MetricValue end) + Sum(case when MetricName=@Metric3 then MetricValue end) + Sum(case when MetricName=@Metric4 then MetricValue end) - Sum(case when MetricName=@Metric2 then MetricValue end) + Sum(case when MetricName=@Metric5 then MetricValue end) - Sum(case when MetricName=@Metric6 then MetricValue end) ) / Sum(case when MetricName=@Metric7 then MetricValue end) CalcResults, @spid FROM Metrics M2 INNER JOIN DataVersion DV ON M2.DataVersionRowID = DV.DataVersionRowID WHERE DV.DataVersionID = @DVID AND MetricName in (@Metric1, @Metric2, @Metric3, @Metric4, @Metric5, @Metric6, @Metric7) Group by DV.ForecastYear)

Are you sure you have no other changes except the order?
There should be no difference whatsoever however you order these lines:

Sum(case when MetricName=@Metric1 then MetricValue end)
+ Sum(case when MetricName=@Metric3 then MetricValue end)
+ Sum(case when MetricName=@Metric4 then MetricValue end)
- Sum(case when MetricName=@Metric2 then MetricValue end)
+ Sum(case when MetricName=@Metric5 then MetricValue end)
- Sum(case when MetricName=@Metric6 then MetricValue end)

Just a thought, if @Metric1 through 6 are always DISTINCT, this should simplify it

Sum(case when MetricName in (@Metric1,@Metric3,@Metric4,@Metric5) then MetricValue end)
- Sum(case when MetricName in (@Metric2,@Metric6) then MetricValue end)

But re your question, the order should not have mattered as all if that is your full query.

0

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

I suggest you to do the aggregations first and then apply the calculations.

SELECT ForecastYear, Metric1, (sum_1 + sum_2 + sum_3 - sum_4 + sum_5 - sum_6) / sum_7, spid FROM ( SELECT DV.ForecastYear, @Metric1 Metric1, SUM(CASE WHEN MetricName = @Metric1 THEN MetricValue END) sum_1, SUM(CASE WHEN MetricName = @Metric3 THEN MetricValue END) sum_2, SUM(CASE WHEN MetricName = @Metric4 THEN MetricValue END) sum_3, SUM(CASE WHEN MetricName = @Metric2 THEN MetricValue END) sum_4, SUM(CASE WHEN MetricName = @Metric5 THEN MetricValue END) sum_5, SUM(CASE WHEN MetricName = @Metric6 THEN MetricValue END) sum_6, SUM(CASE WHEN MetricName = @Metric7 THEN MetricValue END) sum_7, @spid spid FROM Metrics M2 INNER JOIN DataVersion DV ON M2.DataVersionRowID = DV.DataVersionRowID WHERE DV.DataVersionID = @DVID AND MetricName IN (@Metric1,@Metric2,@Metric3,@Metric4, @Metric5,@Metric6,@Metric7) GROUP BY DV.ForecastYear) t1

It really shouldnt matter. With divides, you need to be careful of using integers rather than decimals, so sometimes do a * 1.0 to ensure it uses decimal arithmatic.

The only thing I can think of off the top of my head is maybe NULL values upsetting the aggregate functions.

Using your original, and by the way both cyberkiwi and sharath have good suggestions above...

( Sum(case when MetricName=@Metric1 then MetricValue else 0.0 end)
+ Sum(case when MetricName=@Metric3 then MetricValue else 0.0 end)
+ Sum(case when MetricName=@Metric4 then MetricValue else 0.0 end)
- Sum(case when MetricName=@Metric2 then MetricValue else 0.0 end)
+ Sum(case when MetricName=@Metric5 then MetricValue else 0.0 end)
- Sum(case when MetricName=@Metric6 then MetricValue else 0.0 end)
)

-- should be the same as

( Sum(case when MetricName=@Metric1 then MetricValue else 0.0 end)
- Sum(case when MetricName=@Metric2 then MetricValue else 0.0 end)
+ Sum(case when MetricName=@Metric3 then MetricValue else 0.0 end)
+ Sum(case when MetricName=@Metric4 then MetricValue else 0.0 end)
+ Sum(case when MetricName=@Metric5 then MetricValue else 0.0 end)
- Sum(case when MetricName=@Metric6 then MetricValue else 0.0 end)
)

also your denominator should also be checked, not just for "else 0" but also that it is in fact not zero.

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA).
For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651
If you want to manage em…