Access query with one to many relationship

CliveButler

New Member
Joined
Jun 7, 2014
Messages
14
Hi Everyone,

I'm trying to workout the following. I have 2 tables see below.

There is a one to many relationship between table 1 ID to table 2 ID

What I want is to create a query to select ID number that does not contain any Item starting with B0 or C0

The answer this query should give me is ID103 and ID105

Can anyone help? I spend most of the day on this but not really getting anywhere.

Thanks


Table 1
ID
ID101
ID102
ID103
ID104

<tbody>
</tbody>
Table2
IDItem
ID101A01
ID101A01
ID101B01
ID101B02
ID102B02
ID102B02
ID103D01
ID103E01
ID103E02
ID104C01
ID104B04
ID104C03
ID105A01
ID105A02
ID105A03

<tbody>
</tbody>
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What I want is to create a query to select ID number that does not contain any Item starting with B0 or C0

The answer this query should give me is ID103 and ID105

What about ID101 A01?

Maybe

Code:
SELECT DISTINCTROW [Table2].ID, Left([Item],2) AS Expr1
FROM Table2
GROUP BY [Table2].ID, Left([Item],2)
HAVING (((Left([Item],2)) Not In ("B0","C0")));
 
Last edited:
Upvote 0
What about ID101 A01?

Maybe

Code:
SELECT DISTINCTROW [Table2].ID, Left([Item],2) AS Expr1
FROM Table2
GROUP BY [Table2].ID, Left([Item],2)
HAVING (((Left([Item],2)) Not In ("B0","C0")));

Thanks for the suggestion Fry Girl, but not working unfortunately. The problem is some ID contains mixed Item eg ID101 where it have both A0% and B0%

The query seems to have trouble identifying ID that have mixed Item and include in the result anyway.

so the answer I got is something like

ID101 - because it contains both A0% and B0%
ID103 - correct
ID104 - same reason as ID101
ID105 - correct


ID101 would not be in the answer as it contains B01 and B02.
 
Upvote 0
I don't think you can do this in raw Access SQL, you could in MS-SQL but I don't think access has the required functionality without making the query unnecessarily rigid.
 
Upvote 0
Actually, I'm being slack, you want this I think (you may need to fiddle to get the parentheses in the right place):
Rich (BB code):
SELECT v1.id 
FROM   (SELECT id, 
               Count(item) itm 
        FROM   table2 
        GROUP  BY id) v1 
       LEFT JOIN (SELECT id, 
                         Count(item) itm 
                  FROM   table2 
                  WHERE  item NOT LIKE 'B0%' 
                         AND item NOT LIKE 'C0%' 
                  GROUP  BY id) v2 
              ON v2.id = v1.id 
WHERE  v1.itm = v2.itm

By way of an explanation, the first subquery counts the number of items for each id, the second counts the number of items where the value doesn't begin with 'B0' or 'C0', if the count of items in the first query is equal to the count of items excluding the items you don't want in the second query, then those ids do not contain the excluded items
 
Last edited:
Upvote 0
how about
SELECT tbl1.ID, tbl2.Item FROM tbl1 INNER JOIN tbl2 ON tbl1.ID = tbl2.ID
WHERE (((tbl2.Item) Not Like "B0*" And ((tbl2.Item) Not Like "C0*")));
Worked for me.
 
Upvote 0
That'll return id101 which is incorrect, so it's unfortunately not quite that simple
 
Last edited:
Upvote 0
Using EXISTS:

Code:
SELECT DISTINCT t1.ID 
FROM 
	Table1 t1 
		INNER JOIN 
		Table2 t2
		ON 
		t1.ID = t2.ID
WHERE 
	NOT EXISTS (SELECT * FROM Table2 WHERE t1.ID = ID AND Item LIKE 'B0*')
	AND  NOT EXISTS (SELECT * FROM Table2 WHERE t1.ID = ID AND Item LIKE 'C0*')

I did add ID105 to Table1 because it wasn't in the table!
 
Upvote 0
What I want is to create a query to select ID number that does not contain any Item starting with B0 or C0
Ahh, so the requirement was to not return an ID if in any record it had a corresponding Item that started with B0 or C0.
I didn't take it that way - my fault. Love your solution, xenou! Now I just hope I can retain the concept if ever needed.
 
Upvote 0
Hi everyone,
Thanks for all the suggestions. Here is what ended up using that seems to be working ok

Code:
SELECT DISTINCT T1.ID
FROM TABLE1 AS T1 
LEFT JOIN (SELECT ID, ITEM FROM TABLE1 WHERE ITEM LIKE '[B-C]*')  AS T2 ON T1.ID = T2.ID
WHERE 
    (((T2.ID) Is Null));
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,250
Members
451,757
Latest member
iours

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top