Complex Query Question

CPGDeveloper

Board Regular
Joined
Oct 8, 2008
Messages
190
Hello -- Any help with this would be greatly appreciated:

I have data something like this:

ID, RESULT
1, Y
1, Y
1, Y
2, N
2, N
3, Y
3, N
4, Y
4, N

I only want to pull records where the ID has both a result of 'Y' and a result of 'N' (In this case, that would pull all records with ID 3 and 4) How would I do that in one query?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I'm betting there is a more elegant way to do this. Nevertheless, this will work for your specific question.

SQL:
SELECT *
FROM MyTable
WHERE ID In (SELECT ID FROM MyTable WHERE Result = "Y") And ID In (SELECT ID FROM MyTable WHERE Result = "N")
 
Upvote 0
Instead of one complicated formula, you could try two simple formulas to get the desired result:

QUERY1
SQL:
SELECT DISTINCT Table1.ID, Table1.Result
FROM Table1;

QUERY2
SQL:
SELECT Query1.ID, Count(Query1.ID) AS CountOfID
FROM Query1
GROUP BY Query1.ID
HAVING (((Count(Query1.ID))=2));
 
Upvote 0

Forum statistics

Threads
1,225,351
Messages
6,184,453
Members
453,233
Latest member
bgmb

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