我在 SQL Server 中执行求和查询时遇到问题。
我设置了一个分级价格表。该表的名称称为 TierPricing。
CID RangeID MinValue MaxValue Price Class
1 1 1 5 1.50 1
2 2 6 10 1.25 1
3 3 11 999999999 1.00 1
.
我有一个疑问
DECLARE @QuantityEntered int
DECLARE @ClassEntered int
SET @QuantityEntered = 10;
SET @ClassEntered = 1
SELECT IIf(@QuantityEntered>=TierPricing.MaxValue,TierPricing.MaxValue*TierPricing.Price,(@QuantityEntered-(TierPricing.MinValue-1))*TierPricing.Price)) AS RangePrice
FROM
TierPricing
WHERE
(((TierPricing.MinValue)<=@QuantityEntered) AND ((TierPricing.Class)=@ClassEntered));
当我对 @QuantityEntered 使用值 10 时,就会出现问题。它没有返回 13.75,而是返回 20.00,我不明白为什么。从表中,值 9 到 10 应增加 1.25,从 12.50 增加到 13.75。
进一步说明。
@QuantityEntered = 1 returns 1.50 because 1 falls in between 1 and 5 and 1.50 is added from price field for a total of 1.50
@QuantityEntered = 2 returns 3.00 because 2 falls in between 1 and 5 and 1.50 is added from price field for total of 3.00
@QuantityEntered = 3 returns 4.50 because 3 falls in between 1 and 5 and 1.50 is from price field added for total of 4.50
@QuantityEntered = 4 returns 6.00 because 4 falls in between 1 and 5 and 1.50 is from price field added for total of 6.00
@QuantityEntered = 5 returns 7.50 because 5 falls in between 1 and 5 and 1.50 is from price field added for total of 7.50
@QuantityEntered = 6 returns 8.75 because 6 falls in between 6 and 10 and 1.25 is from price field added for total of 8.75
@QuantityEntered = 7 returns 10.00 because 7 falls in between 6 and 10 and 1.25 is from price field added for total of 10.00
@QuantityEntered = 8 returns 11.25 because 8 falls in between 6 and 10 and 1.25 is from price field added for total of 11.25
@QuantityEntered = 9 returns 12.50 because 9 falls in between 6 and 10 and 1.25 is from price field added for total of 12.50
@QuantityEntered = 10 Should return 13.75 but returns 20.00
我的查询做错了什么?
最佳答案
当我发现你的问题时,我正在尝试自己解决这个问题。我想出了一种基于集合的方法来获得正确的结果,但是您必须更改最小值和最大值以从 0 开始并从较低层的末端开始。所以现在的等级是 0-5、5-10 和 10 - 999999999。这是您的问题的重现:
create table #TierPricing
(
CID int
,RangeId int
,MinValue int
,MaxValue int
,Price money
,Class int
)
insert into #TierPricing(CID, RangeId, MinValue, MaxValue, Price, Class) values (1,1,0,5,1.50,1)
insert into #TierPricing(CID, RangeId, MinValue, MaxValue, Price, Class) values (2,2,5,10,1.25,1)
insert into #TierPricing(CID, RangeId, MinValue, MaxValue, Price, Class) values (3,3,10,999999999,1.00,1)
DECLARE @QuantityEntered int
DECLARE @ClassEntered int
SET @QuantityEntered = 10;
SET @ClassEntered = 1
;with t as (
select
#TierPricing.*
,case
when @QuantityEntered > MaxValue then MaxValue - MinValue --The @QuantityEntered fills up the entire tier
when @QuantityEntered > MinValue then @QuantityEntered - MinValue --The @QuantityEntered partillay fills the tier
else 0
end as TierQuantity
from #TierPricing
)
select
sum(TierQuantity * Price) as RangePrice
from t
drop table #TierPricing
当@QuantityEntered
设置为 10 时,将返回 13.75。
关于sql - 计算分级定价的总和。 SQL 服务器。等级定价表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009489/