计算等级定价中的总和。 SQL Server。层级定价表

时间:2016-08-18 03:21:10

标签: sql sql-server

我在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的表格应从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

我的查询错误了什么?

1 个答案:

答案 0 :(得分:1)

当我发现您的问题时,我正在尝试自己解决此问题。我想出了一种基于集合的方法来获得正确的结果,但是您必须将Min和Max值更改为从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。