WorkingVBA
Registered User.
- Local time
- Today, 12:25
- Joined
- Jul 3, 2013
- Messages
- 33
OK, this is a tricky one, but I am hoping someone else might have seen something like this. I am joining two tables but I need to be able to control (turn on or off) certain records returned by the query. I will try to illustrate:
Table Test1:
K FK Display
1 21 Always
2 22 Always
3 31 Only when Joined
4 32 Always
5 33 Always
6 40 Only when Joined
7 51 Always
8 52 Always
Table Test2
K Data
21 Yes
32 Yes
33 Yes
40 Yes
51 Yes
A simple Join:
SELECT Test1.Key, Test1.FK, Test1.Display, Test2.Data
FROM Test1 LEFT JOIN Test2 ON Test1.FK = Test2.Key;
The results
K FK Display Data
1 21 Always Yes
2 22 Always Null
3 31 Only when Joined Null (This one should not be there)
4 32 Always Yes
5 33 Always Yes
6 40 Only when Joined Yes
7 51 Always Yes
8 52 Always Null
This is the desired result:
K FK Display Data
1 21 Always Yes
2 22 Always Null
4 32 Always Yes
5 33 Always Yes
6 40 Only when Joined Yes
7 51 Always Yes
8 52 Always Null
I have tried a number of things. The actual table Test1 has 27 records of which 19 record should always be displayed in the results regardless of the join, and table Test2 will contain data for about 340 clients each client having any number of records (about 12,000 records in total) that will join to the records in table Test1. It just is that some of those records may be missing.
This is an interesting and challenging problem. I will post the results if I find them myself. At this point I am not sure how to go about solving it and hope I might get some pointers, suggestions and/or ideas from the community.
Thanks
Table Test1:
K FK Display
1 21 Always
2 22 Always
3 31 Only when Joined
4 32 Always
5 33 Always
6 40 Only when Joined
7 51 Always
8 52 Always
Table Test2
K Data
21 Yes
32 Yes
33 Yes
40 Yes
51 Yes
A simple Join:
SELECT Test1.Key, Test1.FK, Test1.Display, Test2.Data
FROM Test1 LEFT JOIN Test2 ON Test1.FK = Test2.Key;
The results
K FK Display Data
1 21 Always Yes
2 22 Always Null
3 31 Only when Joined Null (This one should not be there)
4 32 Always Yes
5 33 Always Yes
6 40 Only when Joined Yes
7 51 Always Yes
8 52 Always Null
This is the desired result:
K FK Display Data
1 21 Always Yes
2 22 Always Null
4 32 Always Yes
5 33 Always Yes
6 40 Only when Joined Yes
7 51 Always Yes
8 52 Always Null
I have tried a number of things. The actual table Test1 has 27 records of which 19 record should always be displayed in the results regardless of the join, and table Test2 will contain data for about 340 clients each client having any number of records (about 12,000 records in total) that will join to the records in table Test1. It just is that some of those records may be missing.
(The join gets even tricky-er when you try to filter out a single client, since the records that are normally shown through the left join with a single client may now be filtered out because they appeared in the results for another client that was not included in the query results)
The data in Test2 is AS-IS so making changes there is not possible. Table Test1 is supposed to "drive" the results and can be modified. This is an interesting and challenging problem. I will post the results if I find them myself. At this point I am not sure how to go about solving it and hope I might get some pointers, suggestions and/or ideas from the community.
Thanks