2009-09-11 44 views
0

我的條件如何找到在SQL Server範圍之間的值

我的查詢返回此

2    10   150000 where 2=startingslab and 10 = endingslab 

11   20   250000 

21   31   150000 

現在我想要得到的價格細節startingslab = 3。即15000.我知道我需要逐行處理它,因爲它們之間沒有工作。

除了遊標和while循環來完成此任何方法嗎?

EDIT

該查詢返回上述結果集

SELECT dbo.TBM_Slab_Details.SlabStartValue, 
     dbo.TBM_Slab_Details.SlabEndValue, 
     convert(int,(@MRP-(dbo.TBM_Slab_Details.Discount*@MRP)/100)) as SlabPrice  
FROM dbo.TBM_SLAB 
     INNER JOIN dbo.TBM_Slab_Details ON dbo.TBM_SLAB.SlabId = dbo.TBM_Slab_Details.SlabId and [email protected] 

現在我有一個可變@slabvalue保持slabvalue。

現在

爲如@ slabvalue = 3,我想150000從上面的結果集

如果是12,我想25萬

+0

你能否擴展你的問題,我不明白 – 2009-09-11 16:08:22

+3

你會提供一些關於你的表結構和你執行的查詢的更多細節嗎? – 2009-09-11 16:08:58

+0

看起來也許第一列是一個包含範圍的字段,他想要檢測給定值落入範圍內的值? – MartW 2009-09-11 16:13:56

回答

0

快瞎炮:

declare @lab int 
select top 1 * 
from yourTable 
where startingslab >= @lab 
order by startingslab asc 
1
SELECT convert(int,(@MRP-(d.Discount*@MRP)/100)) as SlabPrice  
FROM dbo.TBM_SLAB s 
     INNER JOIN dbo.TBM_Slab_Details d ON s.SlabId = d.SlabId and [email protected] 
WHERE @slabValue >= d.SlabStartValue 
     and @slabValue <= d.SlabEndValue 
0

如果你的SLABStartvalue和SlabEnd值是整數(或其他數字)數據類型,也許這會工作?

declare @Myvariable int 
Set @Myvariable = 3 

select @Myvariable, slabprice from 
(SELECT dbo.TBM_Slab_Details.SlabStartValue,   
    dbo.TBM_Slab_Details.SlabEndValue,   
    convert(int,(@MRP-(dbo.TBM_Slab_Details.Discount*@MRP)/100)) as SlabPrice  
FROM dbo.TBM_SLAB   
INNER JOIN dbo.TBM_Slab_Details 
    ON dbo.TBM_SLAB.SlabId = dbo.TBM_Slab_Details.SlabId and [email protected]) a 
    where SlabStartValue <= @Myvariable and SlabEndValue>[email protected] 

如果他們數據的字符類型,你可能需要將它們轉換爲整數where子句中得到它的工作。

相關問題