SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Vlookup in excel translated in SQL query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dhen21dx
Starting Member

4 Posts

Posted - 10/25/2013 :  07:51:07  Show Profile  Reply with Quote
Hi guys,

Please help me, is there any sql syntax where it looks like a vlookup formula in excel. i have 2 tables, Table_A as the source of data and Table_B is the looking up of values. Tables_A&B will be display as a result.

James K
Flowing Fount of Yak Knowledge

3706 Posts

Posted - 10/25/2013 :  08:33:01  Show Profile  Reply with Quote
You can join the two tables:
SELECT a.col1, a.col2, a.col3, b.colx, b.coly
FROM TableA as A inner join TableB as b on a.col1 = b.colx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/25/2013 :  08:47:30  Show Profile  Reply with Quote
No direct equivalent. You can implement functionality using JOIN operation between the tables.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

dhen21dx
Starting Member

4 Posts

Posted - 10/25/2013 :  09:04:07  Show Profile  Reply with Quote
Will i get a result of let say, below example Table_A A1 is my source and im going to look for the value on Table_B B3, the condition is if there is no value to lookup at Table_B B3 then Table_A A1 will still be there and Table_B B3 is blank as a result.

Table_A Table_B
A1 A2 A3 B1 B2 B3 B4
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3706 Posts

Posted - 10/25/2013 :  09:09:30  Show Profile  Reply with Quote
You should ue LEFT JOIN instead of INNER JOIN in the query that I posted earlier; that will give you the row(s) from the source table (Table A) even when there are not matching rows in the right table (Table B).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/25/2013 :  09:20:50  Show Profile  Reply with Quote
And for getting approximate value mode equivalent function iof VLOOKUP in T-SQL you might need to use subquery based on OUTER APPLY

like

SELECT *
FROM TableA a
OUTER APPLY (SELECT TOP 1 B3
             FROM TAbleB
             WHERE B1 <= a.A1
             ORDER BY B3 DESC
             )b


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

dhen21dx
Starting Member

4 Posts

Posted - 10/25/2013 :  09:28:22  Show Profile  Reply with Quote
Thanks a lot.. i got it now.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000