473,946 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reporting a table with two columns from another table

I have a table A, with two ID columns. In a report both ID colums should
be shown with the actual value stored in a second table, B

The problem is, both IDs need to be looked up in B, but are not in the
same row.

How do I do this in an efficient way? A sub select?

Thanks,

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Mar 20 '06 #1
5 1898
Stu
It would help if you posted DDL and some sample data, but it sounds
like you simply need to do a dual join on your second table using an
alias, eg:

SELECT a.*, b1.*, b2.*
FROM tableA a JOIN TableB b1 ON a.ID = b1.ID
JOIN TableB b2 ON aID2 = b2.ID

HTH,
Stu

Mar 20 '06 #2
"Stu" <st************ **@gmail.com> wrote:
It would help if you posted DDL and some sample data, but it sounds
like you simply need to do a dual join on your second table using an
alias, eg:

SELECT a.*, b1.*, b2.*
FROM tableA a JOIN TableB b1 ON a.ID = b1.ID
JOIN TableB b2 ON aID2 = b2.ID


which gives me way too many rows :-) (or I did something very stupid)

I want for each result in a a single row, with each ID (and ID2) in TableB
resolved to an actual value.

SELECT ..., ( SELECT b.value FROM b WHERE a.ID = b.ID )
FROM a, b
WHERE a.ID2 = b.ID
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Mar 21 '06 #3
Stu
Again, without DDL (data Definition Language; Create Table scripts) and
some sample data, it's tough to help...

Nice web pages, BTW.

Stu

Mar 21 '06 #4
John Bokma wrote:
"Stu" <st************ **@gmail.com> wrote:
It would help if you posted DDL and some sample data, but it sounds
like you simply need to do a dual join on your second table using an
alias, eg:

SELECT a.*, b1.*, b2.*
FROM tableA a JOIN TableB b1 ON a.ID = b1.ID
JOIN TableB b2 ON aID2 = b2.ID


which gives me way too many rows :-) (or I did something very stupid)


Maybe you joined the second table only once with a criterion on both id
rows and thus got far less results. But that would be wrong if your
description of the problem was right.

Regards

robert
Mar 21 '06 #5
John Bokma (jo**@castleamb er.com) writes:
"Stu" <st************ **@gmail.com> wrote:
It would help if you posted DDL and some sample data, but it sounds
like you simply need to do a dual join on your second table using an
alias, eg:

SELECT a.*, b1.*, b2.*
FROM tableA a JOIN TableB b1 ON a.ID = b1.ID
JOIN TableB b2 ON aID2 = b2.ID


which gives me way too many rows :-) (or I did something very stupid)

I want for each result in a a single row, with each ID (and ID2) in TableB
resolved to an actual value.


Stu made the assumption that an ID would map to exactly one row in
TableB. Maybe that it is not the case, but how could we know that?

There is a standard recommendation for this kind of questions, and
that is that you post:

o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o Desired result given the sample.

This makes it easy to copy and paste into a query tool to develop a
*tested* solution. Without that information, all you will get is
guesses.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 21 '06 #6

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

Similar topics

14
3460
by: Jeff Boyer | last post by:
Hello everyone, I have recently developed a web application across my clients intranet. We used ASP with Interdev 6.0 and SQL server as the backend. They have now come to me asking me to develop some reports that can be accessed via a drill down within the app. They don't have Crystal Reports so this is not an option. Can anyone suggest an alternative to using crystal reports to publish dynamic reports via the web and ASP?
36
4721
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am proposing a single column table with a field name called vehicle_type and this will contain the vehicle type. Sot it will be
61
24581
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will 'stretch'</td> <td valign="top" width="300">some data that won't 'stetch'</td> </tr> </table>
2
2167
by: Steve Miller | last post by:
hello... i am a 'user' of access, meaning, i import excel files, join, and merge....that's about the extent of my expertise with ms-access. my boss wants me to create an access application that will replace our 'excel' version of time reporting. we use ms-access 2000. i have a huge book (mastering access 2000) though the cd is missing. i looked at using the wizard for time reporting, but is was designed for external clients, and the...
33
2538
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following criteria: I have a list of about 100 numbers which correspond to the ID field and also another 40 say numbers corresponding to the numbers in the PRODUCT field. I want to show the rows that correspond to both these criteria.
6
3880
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson", NamePerson", "AgePerson" and "IDParent". A record contains the information about a person (his name, his...
4
1714
by: Debbiedo | last post by:
I searched the groups and tried several approaches but still cannot find a solution. I have a table that has several hundred fields that may or may not need to be displayed in a report, depending on whether they are NULL or not. (Due to limitations of out software, the table needs to be designed this way) The data needs to be displayed like this:
0
1848
by: Michael Burgess | last post by:
Hi there, I have a problem that's beginning to do my head in and hope someone can help!!! I've got a SQL Server 2005 database which is partly encrypted (certain columns per table are encrypted). I have two different passwords for two different symmetric keys and
0
2604
by: banani | last post by:
Hi Experts, I have couple of questions.. 1) Does anybody know if Reporting Services 2008 will support Rich Text control so that it will interpret html tags? 2) Any idea when Reporting Services 2008 is going to be released? 3) We are using Reporting Services 2005..We have a Matrix report... The problem is the width of the table or the columns do not grow to fit the page. Since the number of the columns is dynamic if it has 2 cols I...
0
10153
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9981
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11558
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
10686
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
9886
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...
1
8248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7423
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();...
2
4533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3539
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.