Results 1 to 9 of 9
  1. #1
    Ira is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21

    Post Help with Join Query to include record from 2 tables

    Hi,
    Running Access 2013 on Windows 8.1

    Need help finishing this query. I know I'm close. I'm getting just one record, but missing all the records from the first table.



    I have a table of Product Prices (Pricing) and a second table of Deviated Prices (DeviatedPrice) for special customers.

    I need to generate a List of Prices for a specific client, but if the PartNo exist in both tables and the client is the one in question (ClientID), have the deviated price written to the table rather than the standard price.

    Any help would be greater appreciated. See code below.

    SELECT Pricing.PartNo, Pricing.StandardPrice, DeviatedPrice.DeviatedPrice
    FROM Pricing LEFT JOIN DeviatedPrice ON Pricing.PartNo = DeviatedPrice.PartNo
    WHERE DeviatedPrice.ClientID=7382;

    If I mess around with the joins, I either get just one record or all the record plus one extra. The deviated price for the client in question is added to the table along with the standard price. I need the deviated price to override the standard price for that client.

    Thank you,
    Ira

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Can you post an example, with sample data from both tables along with your expected output.
    I have a feeling it be a bit clearer if we have a visual view of what you are working with and trying to accomplish.

  3. #3
    Ira is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21
    Sure, See Example below
    Click image for larger version. 

Name:	PricingEx.jpg 
Views:	13 
Size:	43.8 KB 
ID:	30954

  4. #4
    Join Date
    Apr 2017
    Posts
    1,687
    On fly (I don't have Access available to test this):

    Create a saved query p.e.
    qFullPrices: SELECT p.PartNo, p.StandardPrice, dp.DeviatedPrice, dp.ClientID FROM Pricing p LEFT JOIN DeviatedPrice.DeviatedPrice dp ON dp.PartNo = p.PartNo;

    Now you query will be
    Select fp.PartNo, fp.StandardPrice, fp.DeviatedPrice FROM qFullPrices fp WHERE fp.ClientID Is Null OR fp.ClientID = 7382;
    or
    Select fp.PartNo, Nz(fp.DeviatedPrice, fp.StandardPrice) AS Price FROM qFullPrices fp WHERE fp.ClientID Is Null OR fp.ClientID = 7382;

    Edit. Scrap it! This leaves out parts, which have deviated price for some other client, but don't have deviated price for your wanted client! I could make a working query in MS SQL, but I'm afraid this syntax doesn't work in access (where in LEFT JOIN clause you use syntax "... ON tbl1.id1 = tbl2.id1 ... AND tbl1.somefield = somevalue" - I have tried it and did get an error)

  5. #5
    Ira is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21
    I tried it too. Doesn't work. Okay, maybe someone else will have a solution. Thanks anyway.

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Try this:
    Code:
    SELECT Pricing.PartNo, NZ(DeviatedPrice.DeviatedPrice,Pricing.StandardPrice)+0 AS Price
    FROM Pricing 
    LEFT JOIN 
    (SELECT DeviatedPrice.DeviatedPrice, DeviatedPrice,PartNo
    FROM DeviatedPrice
    WHERE DeviatedPrice.ClientID='1234')  AS dp 
    ON Pricing.PartNo = dp.PartNo;

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,956
    A SELECT query does not write data to table. Exactly how do you intend to use the SELECT to accomplish? If a SELECT query can produce the required output when needed, why write to a table?

    Are you trying to save the alternate value to an order record? If you really want to save the alternate value to table, then why not code this behind Order data entry form?

    Regardless, maybe a DLookup() would serve?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Ira is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21

    Solved !!

    Quote Originally Posted by JoeM View Post
    Try this:
    Code:
    SELECT Pricing.PartNo, NZ(DeviatedPrice.DeviatedPrice,Pricing.StandardPrice)+0 AS Price
    FROM Pricing 
    LEFT JOIN 
    (SELECT DeviatedPrice.DeviatedPrice, DeviatedPrice,PartNo
    FROM DeviatedPrice
    WHERE DeviatedPrice.ClientID='1234')  AS dp 
    ON Pricing.PartNo = dp.PartNo;

    Awesome [JoeM]. Works great! Thank you very much

  9. #9
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 07-13-2017, 09:41 AM
  2. Replies: 1
    Last Post: 10-13-2015, 05:28 PM
  3. Join tables and get 'last' record
    By pdauction in forum Queries
    Replies: 1
    Last Post: 04-17-2013, 06:28 AM
  4. Replies: 4
    Last Post: 01-24-2013, 12:11 AM
  5. Replies: 3
    Last Post: 01-05-2012, 12:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums