473,651 Members | 2,765 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 38811
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
2260
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 thought of storing this in SQL were: 1. One table row per day with 48 fields for the readings. 144M table, 3M index saves space and having a long skinny table however, getting the min/max reading in a day is a pain. I
7
4788
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 HIGHLY desirable for our software make use of, but we also support SQL Server. There is no USING option available, and
1
37167
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 SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: 12-14-2004 17:45:55 SQL0204N "TABLE_NAME" is an undefined name.
3
3513
by: serge | last post by:
What is the MS SQL Profiler equivalent in DB2 8.2? Thank you
5
4315
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 of unProcessed (processed value = 0), and Number of Paid vs. Number of unPaid (paid value = 0), I can easily write a query for SQL Server with Case when .... end case statement, I just learned that Access (don't know above which version it...
0
6267
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 using the ArrayList class in ASP.NET to perform a similar role, but am having difficulty in understanding and knowing how to access the data once it has been placed in an ArrayList.
3
4427
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 given below: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SELECT DISTINCTROW Buildings.BuildingNumber,
5
3186
by: sachinkale123 | last post by:
I need sql Equivalent of Append statement...so can any one help in this... thanx
1
1818
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
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1587
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.