计算分层定价的总和。SQL Server。分层定价表

计算分层定价的总和。SQL Server。分层定价表,sql,sql-server,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 我有一个疑问

我在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));

我的查询出了什么问题?

当我发现你的问题时,我正试图自己解决这个问题。我想出了一种基于集合的方法来获得正确的结果,但是您必须将最小值和最大值更改为从0开始,从较低层的末端开始。因此,层现在是0-5、5-10和10-9999999。以下是对您的问题的重新解释:

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。

为什么“@QuantityEntered=10应返回13.75”,而不仅仅是12.50(1.25 x 10)?@Anton。因为第1到第5项的费用为1.50,第6到第10项的费用为1.25。您能在TierPricing中再添加一列,比如TotalPriceAdjustment吗?例如,从6到10的所有项目都有7.50,从11到999999的所有项目都有13.75。然后查询将很简单,公式是:TotalPriceAdjustment+(Value-MinValue+1)x PriceUh。。。你所需要做的就是将
IIf(@QuantityEntered>=
更改为
IIf(@QuantityEntered>
),其实没关系。这就解决了10个问题。实际的解决方法就是将
TierPricing.MaxValue*TierPricing.Price
更改为
(TierPricing.MaxValue-TierPricing.MinValue+1)*TierPricing.Price
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