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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Nearest Min Value using Single SQL

Author  Topic 

kulkarni_ajit
Starting Member

4 Posts

Posted - 2009-03-11 : 06:31:55
Hi

I have table called LTDriveSelectionNew

with Field


ref Voltage

ref1 0.1
ref2 0.2
ref3 0.3
ref4 0.4
ref5 0.5
...... and so on

i want to select vaules Nerest value


for Eg.

Select Ref,Voltage from LTDriveSelectionNew
where voltage= 0.31
then it should display

nearest Min values

ie Ref2 0.3

please reply me it is urgent

waiting for your reply
regards
ajit




SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-11 : 06:40:54
[code]-- Prepare sample data
DECLARE @Sample TABLE
(
ref VARCHAR(10),
Voltage DECIMAL(5, 2)
)

INSERT @Sample
SELECT 'ref1', 0.1 UNION ALL
SELECT 'ref2', 0.2 UNION ALL
SELECT 'ref3', 0.3 UNION ALL
SELECT 'ref4', 0.4 UNION ALL
SELECT 'ref5', 0.5

-- Initialize user supplied parameter
DECLARE @v DECIMAL(5, 2)

SET @v = 0.31

-- Display the result
SELECT TOP 1 WITH TIES
ref,
Voltage
FROM @Sample
ORDER BY ABS(Voltage - @v)[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -