Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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
Master Smack Fu Yak Hacker
3873 Posts
Posted - 2013-10-25 : 08:33:01
You can join the two tables:
SELECT a.col1, a.col2, a.col3, b.colx, b.colyFROM TableA as A inner join TableB as b on a.col1 = b.colx
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2013-10-25 : 08:47:30
No direct equivalent. You can implement functionality using JOIN operation between the tables.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
dhen21dx
Starting Member
4 Posts
Posted - 2013-10-25 : 09:04:07
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
James K
Master Smack Fu Yak Hacker
3873 Posts
Posted - 2013-10-25 : 09:09:30
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).
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2013-10-25 : 09:20:50
And for getting approximate value mode equivalent function iof VLOOKUP in T-SQL you might need to use subquery based on OUTER APPLYlike
SELECT *FROM TableA aOUTER APPLY (SELECT TOP 1 B3 FROM TAbleB WHERE B1 <= a.A1 ORDER BY B3 DESC )b
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs