+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Query Join / Union

  1. #1
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Query Join / Union

    So I have these 2 tables:


    Table1
    ID
    ContractID (Primary)
    Value
    FinishDate

    Table2
    ID(Primary)
    ContractID (allow duplicate)
    AditionalID
    Value
    FinishDate

    The Query should only show the data form the 2 tables if it's in the date range searched.


    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2]);
    How do i change this so that it also takes into account the Table2.FinishDate but at the same time keep Table1.FinishDate, since i have the situation when Table1 data is not the requested date range but Table2 is.

    Thanks for the help
    Last edited by ciprian; 09-14-2011 at 01:48 AM.

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Tricky Query

    Hi

    The easiest thing would be to have 2 queries.

    Since you need to know which one to call, usually you'd use a stored procedure; however Access doesn't directly support these in the UI. They can be used within the VBA environment however, but it depends how comfortable you are in using code.

  3. #3
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Re: Tricky Query

    i thought that would be the case, i have some exp with vba in excel but in access none unfortunately.

    but wouldn't a FULL JOIN help me in this case ? even though access doesn't support it, it would help to know

  4. #4
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Tricky Query

    Wouldn't you need some way of telling Access whether to look at the Table2.FinishDate or Table1.FinishDate? Unless you wanted the same end date in both in which case you could use:

    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2])
    AND ((Table2.FinishDate) Between [Date1] AND [Date2])

  5. #5
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Re: Tricky Query

    the searched FinishDate is the same for both Tables but i have cases when data in Table1 is not valid but the linked data in Table2 is valid.

    If i use AND i won't get any results since the data in Table2 will never be the same FininshDate as in Table1.

    If i use OR all the data in Table2 is showed in the results of the query

  6. #6
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Tricky Query

    In that case you'd need some way of telling Access which Query to run - Stored Procedures

    So you'd need to say run:
    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2])
    or:
    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table2.FinishDate) Between [Date1] AND [Date2])
    Unless I've misunderstood?

    Access can't do this natively so you'd need either 2 queries or use VBA to select which one to run

  7. #7
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Tricky Query

    Actually, try the following, this might work depending on how you want the data displaying:

    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2])
    UNION
    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table2.FinishDate) Between [Date3] AND [Date4])

  8. #8
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Re: Query Join / Union

    I'll try a different approach

    Q1

    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    FROM Table1 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2]);
    Q2

    Select Table2.ContractID, Table2.AditionallID,Table2.Value, Table2.Finishdate, 
    FROM Table2 
    Where ((Table2.FinishDate) Between [Date1] AND [Date2]);
    Separetly these 2 queries give me what i need, how can i merge the results ?
    Last edited by ciprian; 09-13-2011 at 07:31 AM.

  9. #9
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Query Join / Union

    Try:
    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    FROM Table1 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2])
    UNION
    Select Table2.ContractID, Table2.Value, Table2.Finishdate, 
    FROM Table2 
    Where ((Table2.FinishDate) Between [Date3] AND [Date4]);

  10. #10
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Query Join / Union

    Think I may have misunderstood, see my undeleted post, think this should work?

  11. #11
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Re: Query Join / Union

    I made an example, the result in the red balloon should not be there
    Attached Images Attached Images
    Last edited by ciprian; 09-13-2011 at 08:02 AM.

  12. #12
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Query Join / Union

    Is that the result of this?

    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table1.FinishDate) Between [Date1] AND [Date2])
    UNION
    Select Table1.ContractID, Table1.Value, Table1.Finishdate, 
    Table2.AditionalID, Table2.Value, Table2.FinishDate 
    FROM Table1 LEFT JOIN Table2 ON Table1.ContractID=Table2.ContractID 
    Where ((Table2.FinishDate) Between [Date3] AND [Date4])
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  13. #13
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Re: Query Join / Union

    nope, i changed the jpg to show the query from where i get the results

    the union thing does not work for me in this case

  14. #14
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,030

    Re: Query Join / Union

    You only have criteria on Table1, not Table2 so Access is returning all Table 2 and Table1 where it meets your criteria.

    What doesn't work with the SQL UNION query?
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  15. #15
    Registered User
    Join Date
    04-18-2011
    Location
    Bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    64

    Re: Query Join / Union

    same problem with union one

    edit: hmm after i closed the db and opened it again it seems to be working, thanks for the help
    Attached Images Attached Images
    Last edited by ciprian; 09-13-2011 at 08:25 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0