有幾種方法可以做到這一點,也許最簡單的就是產生權重的順序/定在表中的時間間隔目前成本。要做到這一點,我們可以使用數字/理貨表(在這種情況下,我使用master..spt_values
表具有適當的數字範圍)。
因此給予相同的表:
declare @t table (ID int, Weight int, Cost decimal(10,2))
insert @t values (1, 3, 1.00),(2, 10, 2.00),(3, 14, 3.00)
我們可以定義一個查詢(裹着爲了方便公用表表達式)的兩個版本。第一個版本使用lag()
窗口功能,需要一個版本的SQL Server 2012更新比:
;with costs1 (weight, cost) as (
select number, cost
from master..spt_values
inner join (
select isnull(LAG(weight) over (order by id)+1,0) low, weight high, cost from @t
) t on number <= t.high and number >= t.low
where type= 'P'
)
select cost from costs1 where weight = 5;
第二個版本不依賴於lag()
,但使用而不是自聯接:
;with costs2 (weight, cost) as (
select number, cost
from master..spt_values
inner join (
select
isnull(t2.weight + 1,0) as low,
t1.Weight as high,
t1.Cost
from @t t1
left join @t t2 on t1.ID - 1 = t2.ID
) t on number <= t.high and number >= t.low
where type= 'P'
)
select cost from costs2 where weight = 5
另一種選擇是隻計算每個範圍的低/高點,做這樣的查詢:
select cost from (
select isnull(LAG(weight) over (order by id)+1,0) low, weight high, cost from @t
) t
where 5 between low and high
Sample SQL Fiddle與QUER以上。
我沒有得到邏輯。你能解釋一下嗎? – 2015-02-08 09:35:02
我不清楚。擴大測試數據... – 2015-02-08 09:35:50
你的意思是12應該返回2.00?在這種情況下,請選擇top 1 ...,其中weight
2015-02-08 09:37:10