Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i perform excel lookup function in sql.
As per my understanding i can use where condition for searching a value in table as it works as lookup when exact values matched, Here the scenario is different in excel when we perform lookup operation if the looking value is not available in target column it will pick the least value from the same column

EX:- I want to look this value 33.33% in cola and need to return value from colc as 13
in excel am getting correct result. but how do i perform this operation in sql server query

Problem no 1)33.33% not lies in cola values

Problem no 2)How do i pick 13 value from colc , when 33.33% near to 33.42% then i will get 14 from colc
COLA      COLB       COLC
33.06%	  1.06	      13
33.42%	  1.07	      14
33.78%	  1.08	      15


What I have tried:

i tried using where condition
SQL
select colc from tablename where cola=33.33

SELECT  sum(colc)/3 From tablename where round(COLA,0)=33.00 -- randomly tried
Posted
Updated 19-Jun-16 22:23pm
v5

1 solution

Using this example table
SQL
create table tablename
(
	COLA      float,
	COLB       float,
	COLC int
)
insert into tablename values
(33.06,	  1.06,	      13),
(33.42,	  1.07,	      14),
(33.78,	  1.08,	      15)

Probably the easiest way to get the data you want is to order the table by the difference between your input value and COLA. Then just pick the top row returned. I.e.
SQL
declare @lookupval float = 33.33

declare @returnval int = (SELECT TOP 1 COLC FROM tablename 
							ORDER BY ABS( COLA - @lookupval ))


[Edit - apologies I misread this as requiring the closest value, not the closest still lower]
SQL
declare @returnval int = (SELECT TOP 1 COLC FROM tablename 
          WHERE cola < @lookupval ORDER BY COLA)

[Edit 2 - after OP comment]
Combining both my earlier approaches gives the closest value to the input that is lower than the input
SQL
declare @lookupval float = 33.33
declare @returnval int = (SELECT TOP 1 COLC FROM tablename 
	WHERE cola < @lookupval 
	ORDER BY ABS( COLA - @lookupval ))
print @returnval
 
Share this answer
 
v3
Comments
Burning Thoughts 20-Jun-16 4:33am    
i need 13 to be returned.

am getting 14 as per your solution
CHill60 20-Jun-16 4:49am    
Sorry - I misunderstood the question. I've updated my solution
Burning Thoughts 20-Jun-16 5:24am    
how do i make it work on entire table, the solution which is given by you is working fine when only 3 rows. when i use it on actual table it is giving different result
it means top 1 row which started as

col a =0 col b =0 col c= -94

what ever the lookupvalue you pass to it is giving -94 only
CHill60 20-Jun-16 5:46am    
I've combined the approaches - see edited solution
Burning Thoughts 20-Jun-16 5:59am    
Thanks alot..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900