用int列计算MS SQL Server中的百分比。

时间:2022-11-18 01:27:05

I am trying to calculate the total amount for charity. Each row has a bid amount and a charity percentage. The structure of the fields looks like this:

我正在计算慈善机构的总金额。每行有一个投标金额和一个慈善百分比。这些字段的结构如下:

CurrentBid INT CharityPercentage INT <-- 0-100, where 100 is donating all to charity

当前出价INT CharityPercentage INT <- 0-100,其中100人全部捐赠给慈善机构

So i tried this:

所以我试着:

SELECT CurrentBid, CharityPercentage, CurrentBid * (CharityPercentage / 100) AS TotalCharity FROM Items

However it yields this:

然而,收益率:

50  100 50
70  100 70
25  50  0
50  25  0
30  10  0
55  15  0

It works as long as the percentage is 100. Anything below and its 0. My guess is that it has to do with the int column.

只要百分比是100就行。任何低于0的数。我猜这和int列有关。

Any ideas on how to solve it? I have also tried to convert the int using a CAST in the select statement but that did not work. Its a very big procedure to change the INT column to something else.

有什么办法解决它吗?我还尝试使用select语句中的CAST来转换int类型,但这行不通。将INT列更改为其他内容是一个非常大的过程。

2 个解决方案

#1


5  

Explicitly make at least one value a decimal:

显式地使至少一个值为小数:

CurrentBid * CharityPercentage / 100.0 AS TotalCharity

#2


0  

don't CAST -- instead, just multiply by 1.00. This does the CASTing implicitly.

不要施法,只要乘以1。00。这将隐式地进行转换。

SELECT CurrentBid, CharityPercentage, (1.00 * CurrentBid * CharityPercentage) / 100 AS TotalCharity FROM Items

#1


5  

Explicitly make at least one value a decimal:

显式地使至少一个值为小数:

CurrentBid * CharityPercentage / 100.0 AS TotalCharity

#2


0  

don't CAST -- instead, just multiply by 1.00. This does the CASTing implicitly.

不要施法,只要乘以1。00。这将隐式地进行转换。

SELECT CurrentBid, CharityPercentage, (1.00 * CurrentBid * CharityPercentage) / 100 AS TotalCharity FROM Items