This is my script
SELECT City, Country, Density, NULL AS CityAvg
INTO #Temp
FROM CityInfo
UPDATE T1
SET CityAvg = (SELECT STDEV(Density) FROM #Temp T2 WHERE T2.Country = T1.Country)
FROM #Temp T1
When I execute this I get this error
Arithmetic overflow error converting float to data type numeric.
I tried to change NULL to 0.00 but still same error
SELECT City, Country, Density, 0.00 AS CityAvg
INTO #Temp
FROM CityInfo
UPDATE T1
SET CityAvg = (SELECT STDEV(Density) FROM #Temp T2 WHERE T2.Country = T1.Country)
FROM #Temp T1
Data Sample
Country City Density
Australia Melbourne 23.365
Australia Sydney 25.657
Australia Perth 12.374
Canada Toronto 27.748
Canada Montreal 22.559
How to resolve this issue?
Note : This is a very simplified example of my code.
The actual code is more complex so I have to use #Temp table and it has to be in UPDATE statement
densitydata type?#Temptable created via a separateDDLor on-the-fly based on theSELECT INTO? What is the data type of the field in which you are trying to store theCityAvgvalue? What if you explicitly cast yourCityAvgcalculation, something like:CAST(CityAvg AS DECIMAL(38,5)?