Results 1 to 8 of 8

Thread: [RESOLVED] Need help with a querry

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    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.

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: Need help with a querry

    Design the query using Access' query designer and test.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    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

  5. #5
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    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

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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 ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    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?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Re: Need help with a querry

    Its Working

    Many thanks to you guys and to all a merry xmas and a good 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width