Results 1 to 2 of 2

Thread: Left Joins in Microsoft Query

Hybrid View

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Left Joins in Microsoft Query

    I just started a thread in the db forum on whether it's possible to set left joins in excel's 'import external data' query tool and then it occurred to me that the office dev forum might be a better place.

    I'd really apreciate it if anyone who knows much about this tool could have a look and post in the other thread (just to prevent duplicate information).

    Thanks

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Left Joins in Microsoft Query

    Funky
    My preferred approach is to write the Query in the source database and then just reference the resultant query object in MSQuery.


    If that is not an option then another way to solve this is to edit the SQL statement generated by MSQuery. Click on -->View-->SQL to show the SQL.
    You can then change the SQL to use whatever join path you require, I usually do this in SQLServer Query Analyzer. Once your SQL is correct, paste it back into MSQuery. You will get a message stating that
    "SQL Query can't be represented graphically. Continue Anyway?"
    Click OK and it will load you SQL statement.
    Note: Any future changes to the Query will have to be made in the 'raw' SQL statement.

    Here's a before and after version of a query as an example. I was able to use OUTER JOIN in the after and MSQuery accepted the statement.
    Code:
    --Original MS Query SQL
    SELECT PH1_TYPE.Type_Desc
    , PH2_GROUP.Group_Desc
    , PH3_PRODUCT_LINE_PARENT.Product_Desc
    FROM PH1_TYPE PH1_TYPE
    , PH2_GROUP PH2_GROUP
    , PH3_PRODUCT_LINE_PARENT
    WHERE PH1_TYPE.Type_ID = PH2_GROUP.Type_ID 
    AND PH2_GROUP.Group_ID = PH3_PRODUCT_LINE_PARENT.Group_ID
    
    --Revised SQL
    SELECT 	T.Type_Desc
    	,G.Group_Desc
    	,P.Product_Desc
    FROM 	PH3_PRODUCT_LINE_PARENT P
    LEFT OUTER JOIN
    	(
    		PH2_GROUP G
    	LEFT OUTER JOIN
    		PH1_TYPE T
    	ON	G.Type_ID = T.Type_ID 
    	)
    ON	P.Group_ID =G.Group_ID
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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