Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert a SQL subquery into a join when looking at another record in the same table Access 2010
    primarykey
    data
    text
    <p>I have read that Joins are more efficient than subqueries, I have a query that is extremely slow and uses lots of subqueries, therefore I would like to improve it but do not know how.</p> <p>I have the following tables:</p> <pre><code> People \\this table stores lists of individual people with the following fields ( ID, \\Primary Key aacode Text, \\represents a individual house PERSNO number, \\represent the number of the person in the house e.g. person number 1 HRP number, \\the PERSNO of the Housing Reference Person (HRP) the "main" person in the house DVHsize number, \\the number of people in the house R01 number, \\the persons relationship to the person who is PERSNO=1 R02 number, \\the persons relationship to the person who is PERSNO=2 R03 number, \\the persons relationship to the person who is PERSNO=3 AgeCat text, \\the age range of the person e.g. 30-44 xMarSta number, \\representing the marital satus of the person ) Relatives \\this table stores the possible R01 numbers and their text equivalents ( ID Primary Key, \\all possible R01 values Relationship text, \\meaning of the corisponding R01 values ) xMarSta \\this table store the possible xMarSta values and their text equivalents ( ID Primary Key \\all possible xMarSta values Marital text, \\meaning of corresponding R01 values ) </code></pre> <p>The query is:</p> <p><code>HsHld</code> - the goal of this query is to produce for each house (i.e. each aacode) a text sting describing the house in the form <code>[Marital][AgeCat][Relationship][AgeCat][Relationship][AgeCat]</code> etc. So an output for a three person house might look like <code>Married(30-44)Spouse(30-44)Child(1-4)</code></p> <p>I know my current code for HsHld is terrible, but it is included below:</p> <pre><code>SELECT People.ID, People.aacode, People.PERSNO, People.HRP, People.DVHsize, xMarSta.Marital, [Marital] &amp; " (" &amp; [AgeCat] &amp; ")" &amp; [RAL2] &amp; [RAge2] &amp; [RAL3] &amp; [RAge3] &amp; [RAL4] &amp; [RAge4] &amp; [RAL5] &amp; [RAge5] &amp; [RAL6] &amp; [RAge6] &amp; [RAL7] &amp; [RAge7] &amp; [RAL8] &amp; [RAge8] AS HsTyp, (SELECT Fam2.R01 FROM People AS Fam2 WHERE Fam2.aacode = People.aacode AND Fam2.PERSNO = 2) AS Rel2, (SELECT Fam3.R01 FROM People AS Fam3 WHERE Fam3.aacode = People.aacode AND Fam3.PERSNO = 3) AS Rel3, Switch([Rel2] Is Null,Null,[Rel2]=-9,'DNA',[Rel2]=-8,'NoAns', [Rel2]=1,'Spouse',[Rel2]=2,'Cohabitee',[Rel2]&lt;7,'Child', [Rel2]&lt;10,'Parent',[Rel2]&lt;15,'Sibling',[Rel2]=15,'Grandchild', [Rel2]=16,'Grandparent',[Rel2]=17,'OtherRelative', [Rel2]=20,'CivilPartner',True,'Other') AS RAL2, Switch([Rel3] Is Null,Null,[Rel3]=-9,'DNA',[Rel3]=-8,'NoAns', [Rel3]=1,'Spouse',[Rel3]=2,'Cohabitee',[Rel3]&lt;7,'Child', [Rel3]&lt;10,'Parent',[Rel3]&lt;15,'Sibling',[Rel3]=15,'Grandchild', [Rel3]=16,'Grandparent',[Rel3]=17,'OtherRelative', [Rel3]=20,'CivilPartner',True,'Other') AS RAL3, (Select FAge2.AgeCat FROM People AS FAge2 WHERE FAge2.aacode = People.aacode AND FAge2.PERSNO = 2 ) AS RAge2, (Select FAge3.AgeCat FROM People AS FAge3 WHERE FAge3.aacode = People.aacode AND FAge3.PERSNO = 3 ) AS RAge3 FROM Relatives RIGHT JOIN (xMarSta RIGHT JOIN People ON xMarSta.ID=People.xMarSta) ON Relatives.ID=People.R01 WHERE (((People.HRP)=[People.PERSNO])) ORDER BY People.aacode; </code></pre> <p>There are several key things that need to change.</p> <ol> <li>At the moment I can't get a join from the Rel field to the Relatives table to work, so I am using a Switch function called RAL there must be a better way. </li> <li>For simplicity in the post I have only included Rel2 &amp; Rel3 etc but in the actual code it goes up to Rel13! So the problem of performance is even worse.</li> <li>I want to replace these subqueries with joins, but as the subquery looks into another record in the same table I am unsure how to go about this.</li> <li>I'm very out of my depth with this, I know a little SQL but the complexity of this problem is too much for my limited knowledge</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload