473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL's equivalent of Excels VLOOKUP formula??

I've got a data set on which I'm trying to perform the same as excel's
vlookup formula using SQL, but do not know how. Can anyone help?

If you picture me having two tables Table_A and Table_B and they look
like this;
Table_A
Name Sport
John Football
John Rugby
Steve Cycling
Steve Running

Table_B
Name Age
John 23
Steve 24
Peter 25

How do I extract All from Table_B and one Sport from Table_A

I.E I want to end up with something like this...

Name Age Sport
John 23 Football
Steve 24 Cycling
Peter 25 NULL
Thanks in advance,
Ciarán

Jul 23 '05 #1
2 38795
A JOIN between those two tables is what you need. Something like this:

SELECT B.name, A.age, B.sport
FROM Table_B AS B
LEFT JOIN Table_A AS A
ON A.name = B.name

However, your specification is incomplete so the above isn't quite what
you asked for. It isn't obvious why you only showed "Football" for John
and "Cycling" for Steve when each of those names has TWO rows in
Table_A. What is the rule for which row to display? A table is not a
spreadsheet. Tables have no inherent logical order so it isn't good
enough to say you only want to see the "first" row - no such concept
exists unless "first" is defined in your data.

Maybe you just want to show the first sport in alphabetical order, in
which case:

SELECT B.name, A.age, MIN(B.sport)
FROM Table_B AS B
LEFT JOIN Table_A AS A
ON A.name = B.name
GROUP BY B.name, A.age

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
David,

Thanks for your response.
I do not have a rule for which row to display; I want only one record
for each person and any sport associated with that person.

Regards,
Ciarán

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2256
by: Kevin Thorpe | last post by:
I think I know the answer to this, but suggestions welcomed. I have to store lots of information. An electricity reading every half hour for hundreds of meters for several years. The ways I...
7
4784
by: KingGreg | last post by:
All, Oracle 9i provides a "USING" clause option for inner joins, that allows me to say: SELECT * FROM TBL1 JOIN TBL2 USING KeyColumn assuming KeyColumn is in both TBL1 and TBL2. This is...
1
37096
by: Maria | last post by:
Hello we got a script automatically generated to populate a table that belongs to a partition table, then while executing we get this error message: "DB21034E The command was processed as an...
3
3495
by: serge | last post by:
What is the MS SQL Profiler equivalent in DB2 8.2? Thank you
5
4303
by: DaaaDaaa | last post by:
Hi, Here's what I'd like to do: table (Orders) has fields of Processed, Paid (both of boolean yes|no) etc, I want to return a count of of Total number of Orders, Number of Processed vs. Number...
0
6242
by: si | last post by:
I'm very new to using C# in an ASP.NET application, and am used to using the ADO GetRows() method to quickly dump data into a multidimensional array in classic ASP. I am trying to work through...
3
4413
by: s_wadhwa | last post by:
Hi, I'm trying to convert MS Access 97 .mdb application to Access 2003 .adp application with SQL Server as Backend. I'm having trouble converting Access Query into SQL Query. The Query is...
5
3180
by: sachinkale123 | last post by:
I need sql Equivalent of Append statement...so can any one help in this... thanx
1
1807
by: mcolson | last post by:
I tables in Sql, which I am linking to in Access. I have a couple of fields which are using the data type "text". Why are these appearing as the data type "Memo" in Access.
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.