To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Database Development

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Dec 23rd, 2006, 02:15 AM   #1
Bongo
Hyperactive Member
 
Join Date: May 01
Location: Köln
Posts: 385
Bongo is an unknown quantity at this point (<10)
Resolved [RESOLVED] Need help with a querry

Hi guys,

I need help with a querry. (Access DB 2003)

I have 2 Tables

Table1.....Field
===========
..............GuestID
..............GuestName

The Field GuestID is Unique and starts either with the letter 'F' or 'P' depending if the guest is a private person or a company


Table2.....Field
===========
.............BookingID
.............GuestID1
.............GuestID2
.............Arrival
.............Departure

The Fields GuestID1 and GuestID2 are the link to the Table1.GuestID
GuestID1 has alltimes a value GuestID2 could have an entry. This is
depending if on the Invoice has the private and company name on it.


So what I need is a querry which solves the following
Result should look like this

Table2.BookingID
Table1.GuestName - Related to Table2.GuestID1
Table1.GuestName - Related to Table2.GuestID2 (keep in mind this
...................................................................field could be NULL)

the rest I think i can figure out . hopefully

Hope you can help me with this.

many thx in advance and a peacefull xmas season to you all.
Bongo is offline   Reply With Quote
Old Dec 23rd, 2006, 02:22 AM   #2
shakti5385
Just Married
 
shakti5385's Avatar
 
Join Date: Mar 06
Location: Udaipur,Rajasthan(INDIA)
Posts: 3,742
shakti5385 has a spectacular aura about (150+)shakti5385 has a spectacular aura about (150+)
Thumbs up Re: Need help with a querry

Design the query using Access' query designer and test.
__________________
My Blog : My Article of the day
shakti5385 is offline   Reply With Quote
Old Dec 23rd, 2006, 02:26 AM   #3
Bongo
Hyperactive Member
 
Join Date: May 01
Location: Köln
Posts: 385
Bongo is an unknown quantity at this point (<10)
Re: Need help with a querry

Quote:
Originally Posted by shakti5385
Design the query using Access' query designer and test.
thx shakti5383 - this was my first approche but what ever i tried i could not making work.

so back to pure SQL.

Anyone else has a solution for this?

Last edited by Bongo; Dec 23rd, 2006 at 08:05 AM.
Bongo is offline   Reply With Quote
Old Dec 23rd, 2006, 08:24 AM   #4
Bongo
Hyperactive Member
 
Join Date: May 01
Location: Köln
Posts: 385
Bongo is an unknown quantity at this point (<10)
Re: Need help with a querry

I came up sofare with this querry

SELECT Table2.BookingID, T1.Name, T2.Name, Arraival, Derparture
FROM (Table2 INNER JOIN Table1 AS T1 ON Table2.GuestID1 = T1.GuestID) INNER JOIN Table1 AS T2 ON Table2.GuestID2 = T2.GuestID

I only get records
But i still do not get records where on Table2.GuestID2 the Value = NULL
Bongo is offline   Reply With Quote
Old Dec 23rd, 2006, 08:31 AM   #5
shakti5385
Just Married
 
shakti5385's Avatar
 
Join Date: Mar 06
Location: Udaipur,Rajasthan(INDIA)
Posts: 3,742
shakti5385 has a spectacular aura about (150+)shakti5385 has a spectacular aura about (150+)
Thumbs up Re: Need help with a querry

Code:
SELECT Table2.BookingID, T1.Name, T2.Name, Arraival, Derparture
FROM (Table2 LEFT JOIN Table1 AS T1 ON Table2.GuestID1 = T1.GuestID) LEFT JOIN Table1 AS T2 ON Table2.GuestID2 = T2.GuestID
Use Left Join
__________________
My Blog : My Article of the day
shakti5385 is offline   Reply With Quote
Old Dec 23rd, 2006, 08:48 AM   #6
szlamany
MS SQL Powerposter
 
szlamany's Avatar
 
Join Date: Mar 04
Location: CT
Posts: 12,269
szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)
Re: Need help with a querry

This should work (use ALIAS syntax)

Code:
Select TA.BookingId
		,TA.GuestID1
		,T1.GuestName as GuestName1
		,TA.GuestID2
		,T2.GuestName as GuestName2
		,TA.Arrival
		,TA.Departure
	From Table2 TA
	Left Join Table1 T1 on T1.GuestId=TA.GuestID1
	Left Join Table1 T2 on T2.GuestId=TA.GuestID2
This would work in MS SQL - you can join the same table as many times as you want as long as you give them a unique alias name.
__________________

*** Read the sticky in the DB forum about how to get your question answered quickly!! ***

Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

Some Informative Links:
[ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
[ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ VB.Net Database Class ] [ Loading Pictures from DB ]


MS MVP 2006, 2007, 2008
szlamany is offline   Reply With Quote
Old Dec 23rd, 2006, 09:27 AM   #7
Bongo
Hyperactive Member
 
Join Date: May 01
Location: Köln
Posts: 385
Bongo is an unknown quantity at this point (<10)
Re: Need help with a querry

Quote:
Originally Posted by szlamany
This should work (use ALIAS syntax)

Code:
Select TA.BookingId
		,TA.GuestID1
		,T1.GuestName as GuestName1
		,TA.GuestID2
		,T2.GuestName as GuestName2
		,TA.Arrival
		,TA.Departure
	From Table2 TA
	Left Join Table1 T1 on T1.GuestId=TA.GuestID1
	Left Join Table1 T2 on T2.GuestId=TA.GuestID2
This would work in MS SQL - you can join the same table as many times as you want as long as you give them a unique alias name.
@shakti5385 - thx

@szlamany
thx as well but I still get an error which says roughly translatet into english

Syntaxerror (missing operator) on query 'T1.GuestID=TA.GuestID1 ON (Left Join Table1 T2 on T2.GuestId=TA.GuestID2)'

What else I am doing wrong?
Bongo is offline   Reply With Quote
Old Dec 23rd, 2006, 10:09 AM   #8
Bongo
Hyperactive Member
 
Join Date: May 01
Location: Köln
Posts: 385
Bongo is an unknown quantity at this point (<10)
Re: Need help with a querry

Its Working

Many thanks to you guys and to all a merry xmas and a good 2007
Bongo is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Database Development


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:59 AM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.