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.
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.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]);
Thanks for the help
Last edited by ciprian; 09-14-2011 at 01:48 AM.
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.
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
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])
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
In that case you'd need some way of telling Access which Query to run - Stored Procedures
So you'd need to say run:
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 ((Table1.FinishDate) Between [Date1] AND [Date2])
Unless I've misunderstood?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])
Access can't do this natively so you'd need either 2 queries or use VBA to select which one to run
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])
I'll try a different approach
Q1
Q2Select Table1.ContractID, Table1.Value, Table1.Finishdate, FROM Table1 Where ((Table1.FinishDate) Between [Date1] AND [Date2]);
Separetly these 2 queries give me what i need, how can i merge the results ?Select Table2.ContractID, Table2.AditionallID,Table2.Value, Table2.Finishdate, FROM Table2 Where ((Table2.FinishDate) Between [Date1] AND [Date2]);
Last edited by ciprian; 09-13-2011 at 07:31 AM.
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]);
Think I may have misunderstood, see my undeleted post, think this should work?
I made an example, the result in the red balloon should not be there
Last edited by ciprian; 09-13-2011 at 08:02 AM.
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
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
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
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
Last edited by ciprian; 09-13-2011 at 08:25 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks