Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Problem with query


Reply
 
Thread Tools Display Modes
  #1  
Old 03-10-2004, 05:26 AM
noccy noccy is offline
Contributor
 
Join Date: Jun 2003
Location: Oslo, Norway
Posts: 602
Default Problem with query


Hello!

I have the folowing tables:

TblTransactions[Date, Time, SenderAccount, ReceiverAccount]
TblAccounts[Account, Owner]

How can I make a query like this: [Date, Time, SenderAccount, OwnerSenderAccount, ReceiverAccount, OwnerReceiverAccount]?

I want to do a lookup from both SenderAccount and ReceiverAccount, and find the owners...

noccy
Reply With Quote
  #2  
Old 03-10-2004, 06:15 AM
Shurik12 Shurik12 is offline
Steppe Walker

Retired Moderator
* Expert *
 
Join Date: Jul 2002
Location: Ukraine/Russia/Belgium
Posts: 7,227
Default

Hi,

How are you going to relate these two tables to each other if you don't have field (s) to make JOIN on?


Shurik.
__________________
"A diaper is not like a computer that makes satisfying burbling noises from time to time, hinting at great inner complexity." Malcolm Gladwell

"I'm sitting here completely surrounded by no beer." Onslow, 'Keeping up appearances'
Reply With Quote
  #3  
Old 03-10-2004, 06:18 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

you want something like this

Code:
 select t.Date, t.Time, t.SenderAccount, A1.Owner as OwnerSenderAccount,  t.ReceiverAccount, A2.Owner as OwnerReceiverAccount
from TblTransactions t
inner join TblAccounts A1 on t.SenderAccount = A1.Account
inner join TblAccounts A2 on t.ReceiverAccount = A1.Account
Reply With Quote
  #4  
Old 03-10-2004, 06:19 AM
noccy noccy is offline
Contributor
 
Join Date: Jun 2003
Location: Oslo, Norway
Posts: 602
Default

I was thinking about using dlookup in the query, but i am not sure....

The problem is that the transactions table contains two account numbers, and in the query i want to get both accountnumbers, and the account owners (from the accounts table)

noccy
Reply With Quote
  #5  
Old 03-10-2004, 06:40 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

My solution will work because you are setting up two instances of the accounts table one for each of sender and receiver.
Reply With Quote
  #6  
Old 03-10-2004, 06:41 AM
noccy noccy is offline
Contributor
 
Join Date: Jun 2003
Location: Oslo, Norway
Posts: 602
Default

Ok, the inner join seems to work fine, but it only shows med the records where the account number is present in the accounts table.

Is there a way to show all records from tblTransactions?
Reply With Quote
  #7  
Old 03-10-2004, 07:00 AM
DaddyHarris's Avatar
DaddyHarris DaddyHarris is offline
Centurion
 
Join Date: Feb 2004
Location: Alabama
Posts: 194
Default

Quote:
Originally Posted by noccy
Ok, the inner join seems to work fine, but it only shows med the records where the account number is present in the accounts table.

Is there a way to show all records from tblTransactions?
Change the inner join to left outer joins and that should do it for ya...

Enjoy!
__________________
There are no Stupid Questions, only Stupid People
Reply With Quote
  #8  
Old 03-10-2004, 10:08 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

use an outer join rather than an inner join
Reply With Quote
  #9  
Old 03-10-2004, 11:31 AM
noccy noccy is offline
Contributor
 
Join Date: Jun 2003
Location: Oslo, Norway
Posts: 602
Default

Here is what I ended up with:

SELECT t.Date, t.Time, t.SenderAccount, t2.Owner AS SenderAccountOwner, t.ReceiverAccount, tblAccounts.Owner AS ReceiverAccountOwner
FROM tblAccounts RIGHT JOIN (qryTransactions AS t LEFT JOIN tblAccounts AS t2 ON t.SenderAccount = t2.Owner) ON tblTAccounts.AccountNumber = t.ReceiverAccount;


Any views on that solution?

noccy
Reply With Quote
  #10  
Old 03-11-2004, 02:38 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

My preference would be not to use a query in the sql but to use two left outer joins to tblAccounts. What is in the query definition (qryTransactions) ?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query problem yuliam Database and Reporting 12 01-14-2004 07:21 AM
Problem executing a Query wstsmf Database and Reporting 3 10-04-2002 08:59 AM
Problem with Insert Query rpk2006 Database and Reporting 2 05-18-2002 11:46 AM
Problem with SQL Query statement in ADO Roger1 Database and Reporting 4 04-24-2002 03:37 PM
Sql query problem gauri123 Database and Reporting 6 04-03-2002 08:08 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->