简体   繁体   中英

In MS Access, I am getting unexpected result from a query of tables

I have the following tables with the following fields:

  • BeliefT (BeliefID, Beliefs, Topic, Topic2, Topic3, Topic4)
  • ArgumentAgreeT (ReasonToAgreeID, ConclusionA, Reason to agree) The values for these fields, come from lookups to BeliefT.BeliefID and BeliefT.Beliefs .
  • ArgumentDisagreeT (ReasonToDisagreeID, ConclusionD, Reason to Disagree) The values for these fields, also come from lookups to BeliefT.BeliefID and BeliefT.Beliefs .

I have tried everything I can think of. This SQL statement isn't too bad, but it gives me weird results:

SELECT BeliefT.BeliefID, 
       BeliefT.Beliefs, 
       ArgumentsAgreeT.[Reason to agree], 
       ArgumentsDisagreeT.[Reason to Disagree] 
FROM (BeliefT 
      LEFT JOIN ArgumentsAgreeT ON BeliefT.BeliefID = ArgumentsAgreeT.[ConclusionA]) 
LEFT JOIN ArgumentsDisagreeT ON BeliefT.BeliefID = ArgumentsDisagreeT.[ConclusionD];

The above SQL statement includes the the first two things from the first table, and the Reason to agree and Reason to Disagree from the 2nd and 3rd tables. As you can tell from the SQL statement, the join type is an arrow pointing from BeliefT.BeliefID to both ArgumentsAgreeT.ConclusionA and ArgumentsDisagreeT.ConclusionD

The problem is that if there is more than one reason to agree, and only one reason to disagree, that it repeats the reason to disagree for every reason to agree . In other my agree table ( ArgumentAgreeT ) has 5 records. My Disagree table only has 2 records. Whenever there is a record from the Agree table, it duplicates the single record from my disagree table.

Thanks for any help!

First, there are no self-joins here which are multiple references of same table with different aliases in same query.

Second, SQL queries are technically Cartesian products returning the combined set (query) from multiple sets (joined tables) according to a corresponding key. So yes, repeat records as they are paired to the other table will appear as particular combination sets. With 5 X 2 on the same BeliefID, you would naturally yield at least 10 records for every BeliefID.

Also, what did you expect the result to be? The query would not return less than the data that exists. Notice too the entire row is not a duplicate (though records repeat within a column) as each returned combination would be distinct. So even using a DISTINCT will not work.

Possibly, you want to run a Union query that stacks Agree and Disagree items as side by side would not return less than 10 records with table joins for each BeliefID.

SELECT BeliefT.BeliefID, BeliefT.Beliefs, ArgumentsAgreeT.[Reason to agree] As [Reason], 'Agree' As [Type]
FROM BeliefT LEFT JOIN ArgumentsAgreeT ON BeliefT.BeliefID = ArgumentsAgreeT.[ConclusionA]
UNION
SELECT BeliefT.BeliefID, BeliefT.Beliefs,  ArgumentsDisagreeT.[Reason to Disagree] As [Reason], 'Disagree' As Type
FROM BeliefT  LEFT JOIN ArgumentsDisagreeT ON BeliefT.BeliefID = ArgumentsDisagreeT.[ConclusionD];

Finally, do note your issue may not be a query problem but a table design issue. Usually when complex queries are required, data perhaps is not normalized or report requests don't follow data storage structure. The concept of normalization is to group data elements into logical groupings to avoid duplication. Reasons could be its own table with one-to-many link with Beliefs , containing a category/type field to distinguish Agree or Disagree. Doing so, you wouldn't need a union query like the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM