SELECT Query with AND in criteria (1 Viewer)

iXm

New member
Local time
Tomorrow, 02:23
Joined
Mar 27, 2020
Messages
10
I am new to MS Access!

I have a Table, with products and their categories. Each category (CAT) may have no product or multiple numbers of products. Now, I want to search for category which is having P1 and P2 products in it.

I am doing it in SELECT Query, but don't know how to do it..?

Need some help, please.

question.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:23
Joined
May 7, 2009
Messages
19,231
Field: Product----------Product
Show: (tick)------------(no tick)
Criteria: "P1"-----------"P2"
 

iXm

New member
Local time
Tomorrow, 02:23
Joined
Mar 27, 2020
Messages
10
Field: Product----------Product
Show: (tick)------------(no tick)
Criteria: "P1"-----------"P2"


I didn't get you?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:23
Joined
Feb 19, 2013
Messages
16,607
to clarify what you are asking you want to return all CAT's which have both P1 and P2 records

you also need to clarify whether this data needs to be editable or not

I would suggest something like this

SELECT A.CAT
FROM myTable A INNER JOIN myTable B ON A.CAT=B.CAT
WHERE A.Product="P1" AND B.Product="P2"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:23
Joined
May 7, 2009
Messages
19,231
the one I showed you is the one you need to put in Query design (How to do it?, remember). Two Product field in separate column with different criteria.
you also need to use Total query (remove the ID from the query), so it will show only Cat: A
 

bob fitz

AWF VIP
Local time
Today, 22:23
Joined
May 23, 2011
Messages
4,719
The query will return no records with your criteria because no record have P1 AND P2.
Perhaps you need to use OR instead of AND
 
  • Like
Reactions: iXm

iXm

New member
Local time
Tomorrow, 02:23
Joined
Mar 27, 2020
Messages
10
to clarify what you are asking you want to return all CAT's which have both P1 and P2 records

you also need to clarify whether this data needs to be editable or not

I would suggest something like this

SELECT A.CAT
FROM myTable A INNER JOIN myTable B ON A.CAT=B.CAT
WHERE A.Product="P1" AND B.Product="P2"


No, I don't want data to be editable
 

iXm

New member
Local time
Tomorrow, 02:23
Joined
Mar 27, 2020
Messages
10
the one I showed you is the one you need to put in Query design (How to do it?, remember). Two Product field in separate column with different criteria.
you also need to use Total query (remove the ID from the query), so it will show only Cat: A

I did it by pasting your code in SQL View, it works! Can you explain, how to do it in Query Design..?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:23
Joined
Feb 19, 2013
Messages
16,607
in query design, drag your table onto the display area twice - one will have _1 added to the name which is an alias. You can then create your join and criteria and fields to be returned. To change alias names, click on the properties button and select one of the tables - you will see the alias property
 

cheekybuddha

AWF VIP
Local time
Today, 22:23
Joined
Jul 21, 2014
Messages
2,272
Why the join?

More simply:
SQL:
SELECT
  CAT
FROM YourTable
WHERE Product IN ('P1', 'P2')
GROUP BY CAT
HAVING COUNT(*) = 2;
 

isladogs

MVP / VIP
Local time
Today, 22:23
Joined
Jan 14, 2017
Messages
18,209
The query will return no records with your criteria because no record have P1 AND P2.
Perhaps you need to use OR instead of AND

@bob fitz
That won't work here as it will return A. B & C unless you also specify a count >1
 
  • Like
Reactions: iXm

zeroaccess

Active member
Local time
Today, 16:23
Joined
Jan 30, 2020
Messages
671
You could just right click your field area, change to Totals, and change the Group By to Where
 

iXm

New member
Local time
Tomorrow, 02:23
Joined
Mar 27, 2020
Messages
10
to clarify what you are asking you want to return all CAT's which have both P1 and P2 records

you also need to clarify whether this data needs to be editable or not

I would suggest something like this

SELECT A.CAT
FROM myTable A INNER JOIN myTable B ON A.CAT=B.CAT
WHERE A.Product="P1" AND B.Product="P2"


What if I want to go for additional criteria such as "P1" And "P2" And "P3", do I need to have another table named "C"..?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:23
Joined
Feb 19, 2013
Messages
16,607
do I need to have another table named "C".
yes - but it may be simpler to use something like cheeky's

SELECT
CAT
FROM YourTable
WHERE Product IN ('P1', 'P2', 'P3')
GROUP BY CAT
HAVING COUNT(*) = 3;
 

isladogs

MVP / VIP
Local time
Today, 22:23
Joined
Jan 14, 2017
Messages
18,209
Why the join?

More simply:
SQL:
SELECT
  CAT
FROM YourTable
WHERE Product IN ('P1', 'P2')
GROUP BY CAT
HAVING COUNT(*) = 2;

@cheekybuddha
This appeared to be the neatest solution at first sight. However, it doesn't actually work for the original question in post #1 on the supplied data as records 5 & 6 for CAT = C are also wrongly included. It should just be CAT =A.
However, CJL's variation on this idea will work for the modified request from Post #14

I think CJL's solution in post #4 is the most reliable approach overall.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:23
Joined
May 7, 2009
Messages
19,231
it's the Corona effect on the column headings.
 

cheekybuddha

AWF VIP
Local time
Today, 22:23
Joined
Jul 21, 2014
Messages
2,272
@cheekybuddha
This appeared to be the neatest solution at first sight. However, it doesn't actually work for the original question in post #1 on the supplied data as records 5 & 6 for CAT = C are also wrongly included. It should just be CAT =A.
However, CJL's variation on this idea will work for the modified request from Post #14

I think CJL's solution in post #4 is the most reliable approach overall.
Apologies all, unlike Colin, I didn't test. :oops:

The solution is to add Product to the GROUP BY clause.

SQL:
SELECT
  CAT
FROM Test
WHERE Product IN ('P1', 'P2')
GROUP BY
  CAT,
  Product
HAVING COUNT(*) = 2;

Or:
SQL:
SELECT
  CAT
FROM Test
WHERE Product IN ('P1', 'P2', 'P3')
GROUP BY
  CAT,
  Product
HAVING COUNT(*) = 3;
 
Last edited:

Users who are viewing this thread

Top Bottom