0

我用的是db2,不知道是什么版本。尝试对表格求和时出现溢出错误。我认为我可以将总和转换为 BIGINT,这似乎适用于总和,但我希望获得一个百分比,当我转换为 BIGINT 时,我的数据不准确。如何获得 Percent_DeliveredB/A 的准确百分比?将分子和分母转换为 BIGINT 并除以百分比并没有给我正确的结果。

这是我的脚本:

SELECT 
FAT.DIM_BUILDING_ID,
FAT.BUILDING_NAME,
SUM(CAST(FAT.AMOUNT AS BIGINT)) AS SALES_SUM,
SUM(CAST(FAT.ORDERS AS BIGINT)) AS ORDERS_SUM,
SUM(CAST(FAT.CAPABILITY AS BIGINT)) AS CAPABILITY_SUM,
SUM(FAT.ORDERS_B)/sum(FAT.Amount) AS Percent_DeliveredB,
SUM(FAT.ORDERS_A)/sum(FAT.Amount) AS Percent_DeliveredA,
SUM(CAST(FTS.GROUP_A AS BIGINT)) AS GROUP_A,
SUM(CAST(FTS.GROUP_B AS BIGINT)) AS GROUP_B,
SUM(CAST(FTS.GROUP_C AS BIGINT)) AS GROUP_C  

FROM ORDERS AS FAT
INNER JOIN GROUPS AS FTS ON FAT.DIM_PROJECT_ID = FTS.DIM_PROJECT_ID
GROUP BY FAT.DIM_BUILDING_ID, FAT.BUILDING_NAME;

我尝试了以下方法,但百分比返回为 0。

SUM(CAST(FAT.ORDERS_B AS BIGINT))/sum(CAST(FAT.Amount AS BIGINT)) AS Percent_DeliveredB
4

3 回答 3

3

我能够将正确的结果转换为双倍。

SUM(CAST(FAT.ORDERS_B AS DOUBLE))/sum(CAST(FAT.Amount AS DOUBLE)) AS Percent_DeliveredB,
于 2013-09-25T16:10:56.327 回答
2

它给你一个正确的结果,因为任何小于 1 的值,当转换为整数(或 BIGINT )时,显然会被截断为 0。如果您需要小数,请使用 DECIMAL 或 FLOAT 数据类型:

cast(SUM(FAT.ORDERS_B) as decimal(10,2)) / 
cast(sum(FAT.Amount) as decimal(10,2)) AS Percent_DeliveredB

当然,根据您的需要使用正确的精度。

于 2013-09-25T15:03:00.857 回答
1

select count(*)在一张非常大的桌子上使用 a 时,我遇到了同样的错误。

我通过使用count_big(*)which 可以处理超过 2.147.483.647(MAX_LONGINT_INT SQL限制)的计数来解决它。

于 2018-08-30T12:50:13.387 回答