March 5, 2009 at 12:56 pm
I'm not a developer myself but one of my clients is having the following issue in one of our SQL2005 clustered servers (SQL Server 9.0.3042) and I really can't think on anything to help him/her out:
"SUM calculations are not returning correct results when used inside query with tables joined together. In some cases, it is causing a Arithmetic overflow error converting expression to data type int error"
I'm really not sure if this is related to the SQL server itself. There has been no recent patches applied into the machice. We plan to do CU10 + SNAC this coming weekend but that is about it.
Hope anyone can give me a hand on this. Thanks in advance
March 5, 2009 at 1:20 pm
It may not be related to anything added to SQL Server but all Aggregate functions ignore nulls except COUNT(*) which counts all the rows so all aggregate operations with Nullable columns must add COUNT(*).
And OUTER JOINs default to mathematical null so you must also use ISNULL. So give the two links below to your customer lets hope the person understands the math.
http://www.sqlservercentral.com/articles/Advanced+Querying/gotchasqlaggregatefunctionsandnull/1947/
http://www.sqlservercentral.com/articles/Advanced+Querying/2829/
Kind regards,
Gift Peddie
March 6, 2009 at 4:06 pm
thanks for the info mate...the issue was code related indeed
March 6, 2009 at 4:19 pm
I am glad I could help.
Kind regards,
Gift Peddie
March 7, 2009 at 2:33 pm
mario (3/6/2009)
thanks for the info mate...the issue was code related indeed
Two way street, please... what was that code issue and how did you fix it?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 8, 2009 at 1:27 pm
I did not actually fix it but here's what a friend helping me out suggested:
So, I suggest:
a) Recoding the query in such a way that the left outer join only returns the exact number of rows to consider in the result (not sure which is the best way to do that).
b) Use the query # 1 below, splitting the query up in 2 parts: the first one to get the 2 first variables (@EXCP_OC, @DEMAND) and the second part to get the other variable (@ACTUALS)
Hope this helps.
Thanks,
Luis
_______________________
Query # 1 – A possible solution
DECLARE @EXCP_OC Int, @DEMAND Int, @ACTUALS Int
DECLARE @DEMAND_NAME Char(21), @CURR_PERIOD Int
SET @DEMAND_NAME = 'S8PPYCVAR'
SET @CURR_PERIOD = 297480
Select @EXCP_OC = Max(pd.OVERCLOSE),
@DEMAND = Sum(IsNull(pd.DEMAND_REQ,0) + IsNull(pd.DEMAND_ADJUST,0))
From MES_PRODUCT_DEMAND pd
Where RTrim(pd.MKT_PRODUCT_NAME) = RTrim(@DEMAND_NAME)
And pd.PERIOD = @CURR_PERIOD
Select @ACTUALS = Sum(IsNull(tsl.QUANTITY,0))
From MES_PRODUCT_DEMAND pd
Join FA300_SHIPPED_LOTS tsl
On tsl.PERIOD = pd.PERIOD
And tsl.SITE_NAME = pd.SITE_NAME
And tsl.MKT_PRODUCT_NAME = pd.MKT_PRODUCT_NAME
Where RTrim(pd.MKT_PRODUCT_NAME) = RTrim(@DEMAND_NAME)
And pd.PERIOD = @CURR_PERIOD
SELECT @EXCP_OC, @DEMAND, @ACTUALS
Query # 2: Rows returned on each database (run separately on xxx & TESTxxx databases and see the number of rows returned in each case)
DECLARE @EXCP_OC Int, @DEMAND Int, @ACTUALS Int
DECLARE @DEMAND_NAME Char(21), @CURR_PERIOD Int
SET @DEMAND_NAME = 'S8PPYCVAR'
SET @CURR_PERIOD = 297480
Select *
From MES_PRODUCT_DEMAND pd
Left Outer Join FA300_SHIPPED_LOTS tsl
On tsl.PERIOD = pd.PERIOD
And tsl.SITE_NAME = pd.SITE_NAME
And tsl.MKT_PRODUCT_NAME = pd.MKT_PRODUCT_NAME
Where RTrim(pd.MKT_PRODUCT_NAME) = RTrim(@DEMAND_NAME)
And pd.PERIOD = @CURR_PERIOD
Query # 3: Original Query:
DECLARE @EXCP_OC Int, @DEMAND Int, @ACTUALS Int
DECLARE @DEMAND_NAME Char(21), @CURR_PERIOD Int
SET @DEMAND_NAME = 'S8PPYCVAR'
SET @CURR_PERIOD = 297480
Select @EXCP_OC = Max(pd.OVERCLOSE),
@DEMAND = Sum(IsNull(pd.DEMAND_REQ,0) + IsNull(pd.DEMAND_ADJUST,0)),
@ACTUALS = Sum(IsNull(tsl.QUANTITY,0))
From MES_PRODUCT_DEMAND pd
Left Outer Join FA300_SHIPPED_LOTS tsl
On tsl.PERIOD = pd.PERIOD
And tsl.SITE_NAME = pd.SITE_NAME
And tsl.MKT_PRODUCT_NAME = pd.MKT_PRODUCT_NAME
Where RTrim(pd.MKT_PRODUCT_NAME) = RTrim(@DEMAND_NAME)
And pd.PERIOD = @CURR_PERIOD
SELECT @EXCP_OC, @DEMAND, @ACTUALS
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy