我之前在帖子中提出了一个后续问题。 (SQL: Overflow error for type INT)。我现在遇到的问题是将@ SUMX2Y和@ SUMX2X2的结果转换为FLOAT。我尝试过多种不同的方法,但仍有问题。有谁知道我在这里做错了什么?我没有包括我的转换尝试,但它们包括CAST和CONVERT,直到等式的各个级别。
CREATE TABLE #Set1
(
X BIGINT,
Y BIGINT
)
INSERT INTO #Set1
VALUES (220, 630)
,(350, 940)
,(450, 1140)
DECLARE @SUMX BIGINT
DECLARE @SUMY BIGINT
DECLARE @SUMX2 BIGINT
DECLARE @SUMY2 BIGINT
DECLARE @SUMX3 BIGINT
DECLARE @SUMX4 BIGINT
DECLARE @SUMXX BIGINT
DECLARE @SUMXY_ BIGINT
DECLARE @SUMXY BIGINT
DECLARE @SUMXX2 BIGINT
DECLARE @SUMX2Y BIGINT
DECLARE @SUMX2Y_ BIGINT
DECLARE @SUMX2X2 BIGINT
DECLARE @N INT
SET @N = (SELECT COUNT(*) FROM #Set1)
SET @SUMY = (SELECT SUM(Y) FROM #Set1)
SET @SUMX2 = (SELECT SUM(POWER(X,2)) FROM #Set1)
SET @SUMX4 = (SELECT SUM(POWER(X,4)) FROM #Set1)
SET @SUMX2Y_ = (SELECT SUM(POWER(X,2)*Y) FROM #Set1)
SET @SUMX2Y = @SUMX2Y_ - (@SUMX2*@SUMY)/(@N)
SET @SUMX2X2 = @SUMX4 - (POWER(@SUMX2,2)/@N)
PRINT @SUMX2Y
PRINT @SUMX2X2
答案 0 :(得分:0)
我最后只是将BIGINT改为FLOAT。我不确定它是否会导致问题。我们会看到
答案 1 :(得分:0)
使用NUMERIC(18,2)或您想要保留的精度。使用float,这将保留小数精度值,直到您显式传递小数值
下面的代码可以使用
CREATE TABLE #Set1
(
X NUMERIC(18,2),
Y NUMERIC(18,2)
)
INSERT INTO #Set1
VALUES (220, 630)
,(350, 940)
,(450, 1140)
DECLARE @SUMX NUMERIC(18,2)
DECLARE @SUMY NUMERIC(18,2)
DECLARE @SUMX2 NUMERIC(18,2)
DECLARE @SUMY2 NUMERIC(18,2)
DECLARE @SUMX3 NUMERIC(18,2)
DECLARE @SUMX4 NUMERIC(18,2)
DECLARE @SUMXX NUMERIC(18,2)
DECLARE @SUMXY_ NUMERIC(18,2)
DECLARE @SUMXY NUMERIC(18,2)
DECLARE @SUMXX2 NUMERIC(18,2)
DECLARE @SUMX2Y NUMERIC(18,2)
DECLARE @SUMX2Y_ NUMERIC(18,2)
DECLARE @SUMX2X2 NUMERIC(18,2)
DECLARE @N NUMERIC(18,2)
SET @N = (SELECT COUNT(*) FROM #Set1)
SET @SUMY = (SELECT SUM(Y) FROM #Set1)
SET @SUMX2 = (SELECT SUM(POWER(X,2)) FROM #Set1)
SET @SUMX4 = (SELECT SUM(POWER(X,4)) FROM #Set1)
SET @SUMX2Y_ = (SELECT SUM(POWER(X,2)*Y) FROM #Set1)
SET @SUMX2Y = @SUMX2Y_ - (@SUMX2*@SUMY)/(@N)
SET @SUMX2X2 = @SUMX4 - (POWER(@SUMX2,2)/@N)
PRINT @SUMX2Y
PRINT @SUMX2X2
使用以下脚本进行Float&数字差异
DECLARE @SUMX NUMERIC(18,2) = 100
DECLARE @SUMX1 FLOAT = 100
SELECT @SUMX,@SUMX1