百分比计算字段 [英] Percentage calculated field
本文介绍了百分比计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了返回总折扣价的代码.结果集中的数字是正确的,但它们将作为负数返回.
关于如何防止这种情况的任何想法
SELECT Company.CompName AS [公司名称],Products.Prodname AS [产品名称],Orders.ProdQty As 数量,
Orders.ProdQty * Products.Cost As [总成本],(Orders.Discount/ 100 )*( Orders.ProdQty * Products.Cost)-(Orders.ProdQty * Products.Cost) AS [折扣成本]
来自的订单
INNER JOIN 公司 ON Orders.CompanyID = Company.CompanyID
INNER 加入产品打开 Orders.ProdID =产品.ProdCode
订单 BY Company.CompName
结果如下:
Acme Ltd圣诞盒300 5100 -4335
Test Company Ltd圣诞饼干篮530 34450 -31005
Test Company Ltd圣诞饼干纸箱23 1265 -1106.875
Test Company Ltd圣诞盒20 340 -297.5
谢谢
解决方案
尝试一下(看来您忘记了数学的一部分):
(((Orders.Discount/ 100 )*(Orders.ProdQty * Products.Cost))-(Orders.ProdQty * Products.Cost )
谢谢您的回复.
这仍然返回负数.如果这有所不同,则计算中使用的所有字段均为数据类型float.
谢谢
您不能简单地以其他方式使用它(正常价格-折扣金额):
(Orders.ProdQty * Products.Cost)-((Orders.Discount/ 100 )*(Orders.ProdQty * Products.Cost) ) AS [折扣费用]
或简单地:
(Orders.ProdQty * Products.Cost)*(1-(Orders.Discount/ 100 ))
Hi,
I have written code that returns a total discouned price. The figures in the result set are correct, but they are returned as a negative figure.
Any ideas on how prevent this
SELECT Company.CompName AS [Company Name], Products.Prodname AS [Product Name], Orders.ProdQty As Quantity,
Orders.ProdQty * Products.Cost As [Total Cost], (Orders.Discount / 100) * (Orders.ProdQty * Products.Cost) - (Orders.ProdQty * Products.Cost) AS [Discounted Cost]
From Orders
INNER JOIN Company ON Orders.CompanyID = Company.CompanyID
INNER Join Products ON Orders.ProdID = Products.ProdCode
ORDER BY Company.CompName
Reults as below:
Acme Ltd Christmas Box 300 5100 -4335
Test Company Ltd Christmas Cracker Hamper 530 34450 -31005
Test Company Ltd Christmas Cracker Carton 23 1265 -1106.875
Test Company Ltd Christmas Box 20 340 -297.5
Thanks
解决方案
Try this (it looks like you forgot to scope out part of the math):
((Orders.Discount / 100) * (Orders.ProdQty * Products.Cost)) - (Orders.ProdQty * Products.Cost)
Hi Thanks for you reply.
This still returns a negative. All of the fields used in the calculation are of datatype float, if this makes a difference.
Thanks
Shouldn''t you simply have it other way round (normal price - discount amount):
(Orders.ProdQty * Products.Cost) - ((Orders.Discount / 100) * (Orders.ProdQty * Products.Cost)) AS [Discounted Cost]
or simply:
(Orders.ProdQty * Products.Cost) * (1- (Orders.Discount / 100))
这篇关于百分比计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文