473,466 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with multiple joins in MS Access 2007

3 New Member
Hey all - just found this site and I am hoping you can help me with a problem I am having constructing a query for a MS Access 2007 database. I have 3 tables of data simplified as follows
- persons: personID, name
- data1: personID, week, year, place, value
- data2: personID, week, year, value

The persons table is required, but, for a given week and year, the person can have data in either data1, data2, or both. I'd prefer not to combine the data1 and data2 tables since the person will often have many data1 values for a given week/year, but at most 1 data2 value for the same week/year.

I'd like to grab the persons.name, data1.place, data1.value and data2.value for a given week and year as long as there is a value for that week and year in either data1 or data2. Any help would be appreciated!

My current query is as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT persons.name, data1.place, data1.value, data2.value
  2. FROM (persons LEFT JOIN data1 ON (persons.personID = data1.personID AND data1.week = 1 AND data1.year = 2010)) LEFT JOIN data2 ON (persons.personID = data2.personID AND data2.week = 1 AND dats2.year = 2010)
  3.  
Using dummy data in the database, I get the following results:

1) If I have data in data1 for only 1 person but not in data2 for the week/year, it returns ALL the persons, along with the correct data in data1 and data2 (null) - I would prefer to just have persons with values in data1 or data2

2) If I have data in data2 for a given person, but not in data1 for the given week/year, I get a "Join expression not supported" error.

Thanks again for your help!
Oct 1 '10 #1

✓ answered by nico5038

OK, got the idea, but not clear what to do with the PersonValue when multiple are present for one week.

I would start with a UNION query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblData1.PersonID, tblData1.PersonWeek, tblData1.PersonYear, 0 as ActualValue, tblData1.PeronsPlace, tblData1.PersonValue
  2. FROM tblData1
  3. UNION SELECT tblData2.PersonID, tblData2.PersonWeek, tblData2.PersonYear,  tblData2.PersonValue, "",0
  4. FROM tblData2;
  5.  
This will create a row for each data1 and data2 row.

Now this query can be JOINED with the person table like:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblPersons.Name, qryDataUNION.PersonWeek, qryDataUNION.PersonYear, Max(qryDataUNION.ActualValue) AS MaxOfActualValue, Max(qryDataUNION.PeronsPlace) AS MaxOfPeronsPlace, Max(qryDataUNION.PersonValue) AS MaxOfPersonValue
  2. FROM tblPersons INNER JOIN qryDataUNION ON tblPersons.PersonID = qryDataUNION.PersonID
  3. GROUP BY tblPersons.Name, qryDataUNION.PersonWeek, qryDataUNION.PersonYear;
  4.  
The "straight" JOIN will exclude persons with no values in the week.

The group by will group the data into one week showing both the data1 and data2 PersonValue. For the multiple weeks here MAX() is used, but AVG (average) could also be appropriate.

Getting the idea?

Nic;o)

5 2316
nico5038
3,080 Recognized Expert Specialist
The reason to spit data1 and data2 isn't valid from the normalization point of view and is making your query now more complex as needed.
So my advise is to merge these tables.

Next advise is to change the Name and Value fieldnames into e.g. PersonName and PersonValue as Name and Value are reserved words.

To solve your problem in this structure you'll need to add a GROUP BY clause on the personID and week and use a MAX() for the Name and the Value.

Nic;o)
Oct 1 '10 #2
SADude
3 New Member
Sorry, in my attempt to simplify things to make it easier to understand, I actually made it more difficult. Here's a little more info on the problem:

data1 is various hypothetical values gathered from different places. data2 is the actual measured value. So while I could merge these two tables and either have a column like "isActual" or just set the site field to -1 to indicate that this value is the actual value, that seems a little clunky.

Also, I'd like to query the database in such a way as to return recordsets with the person info, the site where the hypothetical value was from, the hypothetical value, and actual value in each row, which I could then easily display in a table. Since some places may not have a hypothetical value for the given week/year, or the actual value may not have been determined for that week/year, the query should be able to handle that and just return null for the missing value. However, if there is no hypotethical value and no actual value for that given time period, I would prefer to not have the person info returned.

Hope that helps clarify the issue and doesn't muddy it further!
Oct 1 '10 #3
nico5038
3,080 Recognized Expert Specialist
OK, got the idea, but not clear what to do with the PersonValue when multiple are present for one week.

I would start with a UNION query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblData1.PersonID, tblData1.PersonWeek, tblData1.PersonYear, 0 as ActualValue, tblData1.PeronsPlace, tblData1.PersonValue
  2. FROM tblData1
  3. UNION SELECT tblData2.PersonID, tblData2.PersonWeek, tblData2.PersonYear,  tblData2.PersonValue, "",0
  4. FROM tblData2;
  5.  
This will create a row for each data1 and data2 row.

Now this query can be JOINED with the person table like:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblPersons.Name, qryDataUNION.PersonWeek, qryDataUNION.PersonYear, Max(qryDataUNION.ActualValue) AS MaxOfActualValue, Max(qryDataUNION.PeronsPlace) AS MaxOfPeronsPlace, Max(qryDataUNION.PersonValue) AS MaxOfPersonValue
  2. FROM tblPersons INNER JOIN qryDataUNION ON tblPersons.PersonID = qryDataUNION.PersonID
  3. GROUP BY tblPersons.Name, qryDataUNION.PersonWeek, qryDataUNION.PersonYear;
  4.  
The "straight" JOIN will exclude persons with no values in the week.

The group by will group the data into one week showing both the data1 and data2 PersonValue. For the multiple weeks here MAX() is used, but AVG (average) could also be appropriate.

Getting the idea?

Nic;o)
Oct 1 '10 #4
SADude
3 New Member
That helped tremendously - it's been so many years since I last tried my hand at database work I had completely forgotten about the uses of the union clause. Thanks for all your help!
Oct 2 '10 #5
nico5038
3,080 Recognized Expert Specialist
Glad I could help and success with your application !

Nic;o)
Oct 2 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Neil | last post by:
A client of mine likes some of the new bells and whistles in Access 2007, and is thinking about converting our A03 format MDB to an A07 format file. However, while some of the users have A07, many...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
4
by: MrEd | last post by:
We unfortunately have multiple versions of Access running, including Access 97. My system was recently upgraded to Office 2007. Now, whenever I open an Access 97 db and subsequently open another...
18
by: Dennis | last post by:
Greetings. I want to run the two versions of Access on the same machine (2003 & 2007). I still need 2003 for most of my work, yet I need to start learning 2007. In my previous attempts at this,...
1
by: rickcross | last post by:
I am trying to use the Access 2007 runtime. I have a program that is fully working in 2007 but when I install the runtime version with same Operating system and Access 2003 installed I have...
4
by: Milan Mehta | last post by:
I am new to Access. I have developed an application in Access 2007. I have kept all the tables in a MDB file and all the forms in a seperate accdb file. All the tables of MDF file is link in the...
1
by: david.triplett | last post by:
I am using MS Access 2007 and have an issue with writing queries against ODBC connected tables. After providing the appropriate ODBC connection string in the properties page, the Show Tables...
3
by: modernshoggoth | last post by:
G'day all, I'm trying to update a single table (containing plans for flights) with information from the same and other tables (containing info for organisations and locations). tblFlightPlans...
0
by: dbdream | last post by:
Hi, I'm having major performance issues in testing with Access 2007 after Access 97 applications are converted (.mdb) format. Loading of forms or reports are 2-3 times slower in Access 2007 than in...
0
by: kalinda | last post by:
I am trying to make multiple joins between two tables and am at a loss. I've tried a variety of things with this being the latest, but it pulls all records multiple times. Basically I have a main...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.