dbTalk Databases Forums  

Keeping rows with the minimum difference between a column and a givenparameter

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Keeping rows with the minimum difference between a column and a givenparameter in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Benjamin Kalytta
 
Posts: n/a

Default Keeping rows with the minimum difference between a column and a givenparameter - 04-30-2010 , 09:34 AM






Hello,

I had no luck with building an sql query to eliminate all rows except
that with MIN(column - @parameter)

Example Table:

RouterId DeviceId Metric Other
------------------------------------------------------
1 1 100 blabla1
1 2 90 blabla2
1 1 50 blabla3
2 1 100 ...
2 1 90 ...
2 1 10 ...

My intention is to compare Metric with a given value like 90 for
example. As we can see, there are rows with equal DeviceId and RouterId.
All that lines (except one) should be removed where DeviceId and
RouterId matches. Only that line with minimum difference between Metric
and given parameter (90) should not be removed.

If we look at RouterId = 1 and DeviceId = 1 we get following rows:

RouterId DeviceId Metric Other
------------------------------------------------------
1 1 100 blabla1
1 1 50 blabla3

So if we compare Metric column with our paramneter, we get following

1. ABS(90 - 100) = 10
2. ABS(90-50) = 40

We want to keep that row with the lowest difference value ... in our
case column 1.

If a general query is not possible, RouterId can be assumed to be known.

SELECT * ... WHERE RouterId = @p1 ... ABS(Metric - @p2) ...

I hope someone can help with with that query.

Some hint: I'm using MS SQL Compact Edition.

Regards,
Benjamin Kalytta

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.