2015-02-07 62 views
-1

我有一個3列的表。其中之一是[Code]。我在這張桌子上有很多記錄。選擇記錄,一列是數字接近10

我想選擇的記錄,他們的[Code]接近定期10 例如數字,如果有選擇的記錄[Code]=9然後選擇記錄有[Code] = 8等等

+0

不夠清楚。不能得到你想要的東西。顯示您的查詢 – 2015-02-07 05:56:38

+0

儘管給了一些示例數據。 – Ajay2707 2015-02-07 05:56:55

+0

我沒有任何疑問,因爲我無法做到這一點 – user3404171 2015-02-07 05:57:17

回答

2

這是基於你的,雖然我實施。

如果您希望接近記錄或記錄標識,而不是值,則只能將條件a.data更改爲a.rid

declare @t table (data int) 

insert into @t values(1), (2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(50),(51),(52) 

declare @value int = 11 , @getDatToValue int = 2 
select * from 
(
    select * , ROW_NUMBER() over(order by data) rid 
    from @t 
) 
a 
where 
a.data between (@value - @getDatToValue) and (@value + @getDatToValue)