Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kulkarni_ajit
Starting Member
4 Posts |
Posted - 2009-03-11 : 07:04:26
|
HiI have table called LTDriveSelectionNewwith Field ref Voltage ref1 0.1ref2 0.2ref3 0.3ref4 0.4ref5 0.5...... and so oni want to select vaules Nerest valuefor Eg.Select Ref,Voltage from LTDriveSelectionNewwhere voltage= 0.31then it should displaynearest Min values ie Ref2 0.3please reply me it is urgentwaiting for your replyregardsajit |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-03-11 : 07:19:16
|
[code]-- *** Test Data ***DECLARE @t TABLE( Ref char(4) NOT NULL ,Voltage decimal(5,1) NOT NULL)INSERT INTO @tSELECT 'ref1', 0.1 UNION ALLSELECT 'ref2', 0.2 UNION ALLSELECT 'ref3', 0.3 UNION ALLSELECT 'ref4', 0.4 UNION ALLSELECT 'ref5', 0.5-- *** End Test Data ***SELECT T.Ref, T.VoltageFROM @t TWHERE T.Voltage =( SELECT MAX(T1.Voltage) FROM @t T1 WHERE T1.Voltage <= 0.31)[/code] |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-03-12 : 05:35:38
|
declare @v as floatSET @v = 3.51SELECT top 1 * FROM mytable ORDER BY ABS(value - @v) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-03-12 : 06:27:06
|
You forget to copy WITH TIES form the link above.Just in case there are two nearest values. E 12°55'05.63"N 56°04'39.26" |
|
|
|
|
|